(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 OSDIR , 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…
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).
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?
), 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:
Cheers,
David
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.