Missing fixtures declarations cause test failures
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# Find missing fixtures declarations from Rails tests # Code by Francois Beausoleil (francois@teksol.info) # Released in the public domain. Do as you wish. desc "Finds missing fixtures declarations from your tests" task :find_missing_fixtures do state = :find_test_case test_case = nil Dir['test/*/*_test.rb'].each do |file| File.open(file, 'r') do |f| f.each do |line| case state when :find_fixture case line when /def test_/ printf "%s: %s\n", file, test_case state = :find_test_case when /fixtures/ state = :find_test_case when /class (\w+) < Test::Unit::TestCase$/ test_case = $1 state = :find_test_case end when :find_test_case case line when /class (\w+) < Test::Unit::TestCase$/ test_case = $1 state = :find_fixture end end end end end end |
1 2 3 4 5 |
$ rake find_missing_fixtures (in D:/wwwroot/wpul.staging.teksol.info) test/unit/name_test.rb: NameTest test/unit/application_helper_test.rb: ApplicationHelperTest 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…