2011-06-27 40 views
4

嘗試使用早該編寫一個簡單的單元測試和軌道3.早該上單元的超類不匹配測試

測試/單元/ 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 

相關部分的Gemfile

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

當我嘗試使用rake test --trace我收到以下錯誤運行此:

** 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 

我明白這個錯誤,我只是不明白在哪裏另一個UserTest類將被定義給我這個問題。有什麼想法嗎?

邁克

+0

是什麼'找到。 | xargs grep -l UserTest'返回?從項目的根目錄運行它。 –

+0

哇我是白癡 - 原來我已經複製從這個單元測試代碼分成因此限定UserTest兩次另一個單元測試。非常感謝! –

+0

我想這是一個比較常見的錯誤;) –

回答

5

檢查的find . | xargs grep -l UserTest輸出對類名的意外重複使用。

+0

謝謝s.m--那就是訣竅! :) –

+1

您可能需要一個'-name「* .rb''添加到您的'find' –

0

我能想象避免這種錯誤的唯一方法是通過執行以下操作:

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 

,如果你要重複

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

你會得到

warning: already initialized constant UserTest 

但這種方法看起來有點奇怪。

+0

是啊 - 這是可行的,但它是矯枉過正一個更微妙的問題:愚蠢的編程。我正在爲此做一個修復,但尚未得到正確的結果。 :) –

+0

@Mike:創建時發給我補丁! :) –