This project is archived and is in readonly mode.
Fixtures names can't start with 'test'
-
Menno
- Title changed from ArgumentError using fixtures named test... to ArgumentError using fixtures named test*
-
Menno
It turns out things are messed up by line 54 of Ruby 1.8.6's test/unit/testcase.rb:
tests = method_names.delete_if {|method_name| method_name !~ /^test./}This is in the self.suite method which "Rolls up all of the test* methods in the fixture into one suite, creating a new instance of the fixture for each method."
When you remove your fixture names from method_names, i.e. add a line tests.delete("test_whatevers") after line 54, all is fine again. But obviously this is no solution.
-
Menno
- Title changed from ArgumentError using fixtures named test* to Fixtures names can't start with 'test'
-
oddlyzen
wouldn't it be better (instead of checking the method_name string) to check the module/class hierarchy for the testing module..? checking the string/name assumes that everyone is going to follow the conventions. we know that's not true, likely, or maybe even possible.
-
josh
- State changed from new to wontfix
- Tag set to 2.0-stable, actionpack, activerecord, bug
-
Menno
Thanks for the update. Should I interpret the tags as "fixed in 2.1"?
-
Menno
- Tag changed from 2.0-stable, actionpack, activerecord, bug to 2.0-stable, actionpack, activerecord, bug
Issue still present in Rails 2.1.1
-
Rob Lucas
I'm getting this bug as a result of having a model called "Testimonial". It seems that I can't have fixtures at all for this model because of this bug. Surely we can handle these things more intelligently than this!
-
Rob Lucas
Rails apparently regards "Test" as a reserved name for controllers, probably for this kind of reason. When trying to generate a controller just called "Test", I get the following response in Rails 2.1.1:
The name 'Test' is either already used in your application or reserved by Ruby on Rails.
Please choose an alternative and run this generator again.
Suggestions:
trial trial run tryout mental test mental testing psychometric test examination exam run prove try try out examine essay screen
quiz
But models with names such as "testimonial", "testament" etc should certainly still be possible, and I can't really see why something like "test_driver" shouldn't work either. I've managed to get around this problem by hacking the line of test/unit/testcase.rb that is mentioned by Menno above. Rather than going in and hacking the actual original file, I'm overriding it by requiring a file in my troubled unit tests like:
module Test module Unit class TestCase # Require this file in any unit tests for models starting with the letters "test". # Overrides the suite method in Ruby's test/unit/testcase.rb so that we don't end # up trying to run a spurious unit test method of the same name as our model def self.suite method_names = public_instance_methods(true) tests = method_names.delete_if {|method_name| method_name !~ /^test./ || method_name == "test_whatevers"} suite = TestSuite.new(name) tests.sort.each do |test| catch(:invalid_test) do suite << new(test) end end if (suite.empty?) catch(:invalid_test) do suite << new("default_test") end end return suite end end end end -
Rob Lucas
Rails apparently regards "Test" as a reserved name for controllers, probably for this kind of reason. When trying to generate a controller just called "Test", I get the following response in Rails 2.1.1:
The name 'Test' is either already used in your application or reserved by Ruby on Rails.
Please choose an alternative and run this generator again.
Suggestions:
trial trial run tryout mental test mental testing psychometric test examination exam run prove try try out examine essay screen quiz
But models with names such as "testimonial", "testament" etc should certainly still be possible, and I can't really see why something like "test_driver" shouldn't work either. I've managed to get around this problem by hacking the line of test/unit/testcase.rb that is mentioned by Menno above. Rather than going in and hacking the actual original file, I'm overriding it by requiring a file in my troubled unit tests like:
module Test module Unit class TestCase # Require this file in any unit tests for models starting with the letters "test". # Overrides the suite method in Ruby's test/unit/testcase.rb so that we don't end # up trying to run a spurious unit test method of the same name as our model def self.suite method_names = public_instance_methods(true) tests = method_names.delete_if {|method_name| method_name !~ /^test./ || method_name == "test_whatevers"} suite = TestSuite.new(name) tests.sort.each do |test| catch(:invalid_test) do suite << new(test) end end if (suite.empty?) catch(:invalid_test) do suite << new("default_test") end end return suite end end end end -
Mike Boone
- Tag changed from 2.0-stable, actionpack, activerecord, bug to 2.0-stable, actionpack, activerecord, bug
There is still a problem in Rails 2.3.5 with creating a model beginning with 'Test'.
rails myapp
cd myapp
script/generate model testimonial name:string comment:text
rake db:migrate
rake test:units1) Error: testimonials(ActionView::TestCase):
TypeError: wrong argument type Class (expected Module)8 tests, 1 assertions, 0 failures, 1 errors
rake aborted! -
Hery
- Tag changed from 2.0-stable, actionpack, activerecord, bug to 2.0-stable, actionpack, activerecord, activesupport, bug, testcase
Hi, I found the fix for rails 3 and test-unit using minitest. It is minitest bug :
In minitest/unit.rb
def self.test_methods methods = public_instance_methods(true).grep(/^test/).map { |m| m.to_s }.sort if self.test_order == :random then max = methods.size methods = methods.sort_by { rand(max) } end methods endYou just have to patch your test/test_helper.rb
class ActiveSupport::TestCase # Setup all fixtures in test/fixtures/*.(yml|csv) for all tests in alphabetical order. # # Note: You'll currently still have to declare fixtures explicitly in integration tests # -- they do not yet inherit this setting fixtures :all # Fix minitest bug : all public methods beginning with 'test' are considered as test_methods ... that is not true if we have a resource called testimony # testimony_path will not be found and tests will fail! def self.test_methods methods = public_instance_methods(true).grep(/^test$|^test_/).map { |m| m.to_s }.sort if self.test_order == :random then max = methods.size methods = methods.sort_by { rand(max) } end methods end # Add more helper methods to be used by all tests here... endShould it be included in ActiveSupport::TestCase ?
-
chris finne
Workaround is pretty easy. Just rename your fixtures file, then associate that odd name with the proper class.
testimonials.yml => atestimonials.yml
and in test/test_helper.rb
class ActiveSupport::TestCase ... set_fixture_class :atestimonials => Testimonial ... end -
Mike Boone
@Chris, thanks for the workaround. I tried it and it works.
I still don't understand why this is a "wontfix". Seems a legitimate bug. Unless there is documentation that states "don't name models with any word that begins with 'Test'"?
-
Jeff Kreeftmeijer
Automatic cleanup of spam.