Bob Silva posted Testing Gotchas in Rails in mid-October. I had fallen across this error myself a few times.
Well, today I had a huge code base I needed to check. I wrote the following Rake task:
lib/tasks/find_missing_fixtures.rake
1 # Find missing fixtures declarations from Rails tests
2 # Code by Francois Beausoleil (francois@teksol.info)
3 # Released in the public domain. Do as you wish.
4 desc "Finds missing fixtures declarations from your tests"
5 task :find_missing_fixtures do
6 state = :find_test_case
7 test_case = nil
8
9 Dir[‘test//_test.rb’].each do |file|
10 File.open(file, ‘r’) do |f|
11 f.each do |line|
12 case state
13 when :find_fixture
14 case line
15 when /def test_/
16 printf "%s: %s\n", file, test_case
17 state = :find_test_case
18 when /fixtures/
19 state = :find_test_case
20 when /class (\w) < Test::Unit::TestCase$/
21 test_case = $1
22 state = :find_test_case
23 end
24
25 when :find_test_case
26 case line
27 when /class (\w) < Test::Unit::TestCase$/
28 test_case = $1
29 state = :find_fixture
30 end
31 end
32 end
33 end
34 end
35 end
Run it like this:
1 $ rake find_missing_fixtures
2 (in D:/wwwroot/wpul.staging.teksol.info)
3 test/unit/name_test.rb: NameTest
4 test/unit/application_helper_test.rb: ApplicationHelperTest
5 test/unit/application_helper_test.rb: ApplicationHelperTruncationTest
Why do I report both the file and TestCase name ? Because I put more than one TestCase per test file, as I reported in Test fixtures and behavioral testing
Of course, if I could do away with fixtures altogether…