Jo Liss
2010-Nov-30 16:09 UTC
[rspec-users] RSpec interacting with Test::Unit: weird "is already defined" error
Hey everyone, (I''m a newbie, so if I''m doing something wrong, please let me know! ^^) I''m getting weird errors when I mix RSpec and Test::Unit tests. Here is how to reproduce it with Rails 3: Run "rails new railsapp". Add "gem ''rspec''" to the Gemfile, and create test/unit/demo_test.rb with the following code: require ''test_helper'' class FirstTest < ActiveSupport::TestCase test ''something'' do assert_equal 42, 42 end end class SecondTest < ActiveSupport::TestCase test ''something'' do assert_equal 42, 42 end end Run "bundle install; rake db:migrate; rake test". The two tests pass. Now add the following code to the end of test/unit/demo_test.rb: describe ''this is an rspec test'' do end Run "rake test" again, and it fails with the following errors message: (in /home/jo/tmp/railsapp) /var/lib/gems/1.9.1/gems/activesupport-3.0.1/lib/active_support/testing/declarative.rb:28:in `test'': test_something is already defined in FirstTest (RuntimeError) from /home/jo/tmp/railsapp/test/unit/demo_test.rb:4:in `<class:FirstTest>'' from /home/jo/tmp/railsapp/test/unit/demo_test.rb:3:in `<top (required)>'' from /var/lib/gems/1.9.1/gems/activesupport-3.0.1/lib/active_support/dependencies.rb:235:in `load'' from /var/lib/gems/1.9.1/gems/activesupport-3.0.1/lib/active_support/dependencies.rb:235:in `block in load'' from /var/lib/gems/1.9.1/gems/activesupport-3.0.1/lib/active_support/dependencies.rb:227:in `load_dependency'' from /var/lib/gems/1.9.1/gems/activesupport-3.0.1/lib/active_support/dependencies.rb:235:in `load'' from /var/lib/gems/1.9.1/gems/rspec-core-2.0.1/lib/rspec/core/configuration.rb:306:in `block in load_spec_files'' from /var/lib/gems/1.9.1/gems/rspec-core-2.0.1/lib/rspec/core/configuration.rb:306:in `map'' from /var/lib/gems/1.9.1/gems/rspec-core-2.0.1/lib/rspec/core/configuration.rb:306:in `load_spec_files'' from /var/lib/gems/1.9.1/gems/rspec-core-2.0.1/lib/rspec/core/command_line.rb:18:in `run'' from /var/lib/gems/1.9.1/gems/rspec-core-2.0.1/lib/rspec/core/runner.rb:55:in `run_in_process'' from /var/lib/gems/1.9.1/gems/rspec-core-2.0.1/lib/rspec/core/runner.rb:46:in `run'' from /var/lib/gems/1.9.1/gems/rspec-core-2.0.1/lib/rspec/core/runner.rb:10:in `block in autorun'' Errors running test:units! I googled for the error message and came up with http://osdir.com/ml/RubyonRails:Core/2009-12/msg00084.html , which tells me that since "FirstTest" and "SecondTest" have different class names, there shouldn''t be a problem. Am I doing something wrong? I''m not even sure whether this is a Test::Unit or RSpec problem, but since RSpec seems to trigger it, I figured I''d post here... Thanks, Jo -- Posted via http://www.ruby-forum.com/.
Jo Liss
2010-Nov-30 16:28 UTC
[rspec-users] RSpec interacting with Test::Unit: weird "is already defined" error
One more thing: If instead of "test ''something''" I write "def test_something", like so: require ''test_helper'' class FirstTest < ActiveSupport::TestCase def test_something assert_equal 42, 84 end end class SecondTest < ActiveSupport::TestCase def test_something assert_equal 42, 84 end end describe ''this is an rspec test'' do end ... then adding the last two lines causes the two test_something tests to not be picked up at all (so zero tests are run). Strange, isn''t it? Jo -- Posted via http://www.ruby-forum.com/.
David Chelimsky
2010-Nov-30 18:21 UTC
[rspec-users] RSpec interacting with Test::Unit: weird "is already defined" error
On Nov 30, 2010, at 10:28 AM, Jo Liss wrote:> One more thing: If instead of "test ''something''" I write "def > test_something", like so: > > require ''test_helper'' > class FirstTest < ActiveSupport::TestCase > def test_something > assert_equal 42, 84 > end > end > class SecondTest < ActiveSupport::TestCase > def test_something > assert_equal 42, 84 > end > end > describe ''this is an rspec test'' do > end > > ... then adding the last two lines causes the two test_something tests > to not be picked up at all (so zero tests are run). > > Strange, isn''t it?Hey Jo - welcome to the RSpec list. RSpec and Test::Unit each have their own runners which work differently, so what you''re experiencing is not surprising. Are you just exploring at this point? Or is there a problem you''re trying to solve by intermingling them? I''d recommend keeping specs under the spec directory and run them separately from Test::Unit tests.
Jo Liss
2010-Nov-30 20:11 UTC
[rspec-users] RSpec interacting with Test::Unit: weird "is already defined" error
On Tue, Nov 30, 2010 at 7:21 PM, David Chelimsky <dchelimsky at gmail.com> wrote:> Or is there a problem you''re trying to solve by intermingling them? > > I''d recommend keeping specs under the spec directory and run them separately from Test::Unit tests.Ah, I see -- thanks for the quick reply! So, I''m really trying to write test code, not specs. There are a bunch of existing Test::Unit tests. But since I''m using rspec/expectations''s "should" assertions in my test code already (as described on http://relishapp.com/rspec/rspec-expectations/v/2-0/dir/test-frameworks/test-unit-integration ), I figured I might use RSpec instead of Test::Unit to structure my tests, as RSpec has this awesome nesting feature. But from your reply I take it that trying to squeeze my test code into RSpec (even if I migrated all my Test::Unit tests to RSpec tests) is going to result in ugly code and unhappiness? Jo
David Chelimsky
2010-Nov-30 20:39 UTC
[rspec-users] RSpec interacting with Test::Unit: weird "is already defined" error
On Nov 30, 2010, at 2:11 PM, Jo Liss wrote:> On Tue, Nov 30, 2010 at 7:21 PM, David Chelimsky <dchelimsky at gmail.com> wrote: >> Or is there a problem you''re trying to solve by intermingling them? >> >> I''d recommend keeping specs under the spec directory and run them separately from Test::Unit tests. > > Ah, I see -- thanks for the quick reply! > > So, I''m really trying to write test code, not specs. There are a > bunch of existing Test::Unit tests. But since I''m using > rspec/expectations''s "should" assertions in my test code already (as > described on http://relishapp.com/rspec/rspec-expectations/v/2-0/dir/test-frameworks/test-unit-integrationThat page only applies to rspec-expectations (should + matchers), not rspec-core, which provides the structure.> ), I figured I might use RSpec instead of Test::Unit to structure my > tests, as RSpec has this awesome nesting feature.Yes, it does, which is why I like to use rspec for the whole thing :)> But from your reply I take it that trying to squeeze my test code into > RSpec (even if I migrated all my Test::Unit tests to RSpec tests) is > going to result in ugly code and unhappiness?Not necessarily. Depends on how you go about it. Also depends on what environment you''re already in. There is a project called rspec-unit that can help you migrate from test/unit to rspec gradually, but it doesn''t always work well if you''re using test/unit extensions that hack into test/unit internals. Might want to take a look at it, however: https://github.com/glv/rspec-unit Cheers, David> > Jo
Jo Liss
2010-Nov-30 21:35 UTC
[rspec-users] RSpec interacting with Test::Unit: weird "is already defined" error
On Tue, Nov 30, 2010 at 9:39 PM, David Chelimsky <dchelimsky at gmail.com> wrote:> Yes, it does, which is why I like to use rspec for the whole thing :)I see... In that case I think I''ll go ahead and try migrating all my tests to RSpec. And this ...> https://github.com/glv/rspec-unit... will definitely help on the way. :) Thanks for the pointer! Jo