4

Trying to write a simple unit test using shoulda and rails 3.

test/unit/user_test.rb

class UserTest < Test::Unit::TestCase
  should validate_presence_of(:password, :on => :create)
  should validate_presence_of(:handle, :email)
  should validate_confirmation_of(:password)
  should validate_length_of(:handle, :within => 6..15)
  should validate_uniqueness_of(:handle)
  should validate_format_of(:handle, :with => /\A\w+\z/i)
  should validate_length_of(:email, :within => 6..100)
end

Relevant parts of Gemfile

group :test do
  gem 'shoulda'
  gem 'rspec-rails', '2.0.0.beta.12'
end

When I try to run this using rake test --trace I receive the following error:

** Execute test:units
/Users/removed/removed/removed/app_name/test/unit/user_test.rb:5: superclass mismatch for class UserTest (TypeError)
    from /Library/Ruby/Gems/1.8/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:239:in `require'
    from /Library/Ruby/Gems/1.8/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:239:in `require'
    from /Library/Ruby/Gems/1.8/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:227:in `load_dependency'
    from /Library/Ruby/Gems/1.8/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:239:in `require'
    from /Library/Ruby/Gems/1.8/gems/rake-0.9.2/lib/rake/rake_test_loader.rb:9
    from /Library/Ruby/Gems/1.8/gems/rake-0.9.2/lib/rake/rake_test_loader.rb:9:in `each'
    from /Library/Ruby/Gems/1.8/gems/rake-0.9.2/lib/rake/rake_test_loader.rb:9
    from /Library/Ruby/Gems/1.8/gems/rake-0.9.2/lib/rake/rake_test_loader.rb:5:in `each'
    from /Library/Ruby/Gems/1.8/gems/rake-0.9.2/lib/rake/rake_test_loader.rb:5

I understand the error, I just don't get where another UserTest class would be defined that's giving me this issue. Any thoughts?

Mike

Mike Sukmanowsky
  • 3,481
  • 3
  • 24
  • 31

2 Answers2

5

Check the output of find . | xargs grep -l UserTest against accidental duplicated uses of the class name.

s.m.
  • 7,895
  • 2
  • 38
  • 46
0

The only way I could imagine avoiding this mistake is by doing the following:

UserTest = Class.new(Test::Unit::TestCase)
class UserTest # Or class UserTest < Test::Unit::TestCase is also allowed
  should validate_presence_of(:password, :on => :create)
  should validate_presence_of(:handle, :email)
  should validate_confirmation_of(:password)
  should validate_length_of(:handle, :within => 6..15)
  should validate_uniqueness_of(:handle)
  should validate_format_of(:handle, :with => /\A\w+\z/i)
  should validate_length_of(:email, :within => 6..100)
end

if you were to repeat

UserTest = Class.new(Test::Unit::TestCase) # repeated

you'd get

warning: already initialized constant UserTest

But this approach would look a little weird.

Andrew Grimm
  • 78,473
  • 57
  • 200
  • 338