Ruby Noob - 我搜索了SO,讀了Rails Guide on Testing,並且看了RailsCast沒有運氣。模型和測試中的驗證方法之間的錯誤:單元斷言結果
我無法調和方法與我寫的測試的關聯。目前(代碼如下),這兩個測試,一個檢查名稱的存在和一個檢查電子郵件的存在,通過沒有失敗或錯誤 - 好吧,看起來不錯。
如果我從模型中刪除兩個驗證語句,那麼這兩個測試都會失敗 - 也是有意義的。
但是,如果我只刪除任
validates :name, :presence => true
OR
validates :email, :presence => true
從模型ONE,BOTH測試通過無故障。這個結果對我來說沒有意義。我是否創建了錯誤的測試?
user.rb型號:
class User < ActiveRecord::Base
attr_accessible :email, :name
has_many :connections
validates :name, :presence => true
validates :email, :presence => true
end
測試/單位/ user_test.rb
require 'test_helper'
class UserTest < ActiveSupport::TestCase
test "a user should have a name" do
user = User.new
assert !user.save
end
test "a user should have an email" do
user = User.new
assert !user.save
end
end
你是正確的,我沒有測試單個前提(或屬性)。我沒有提及我正在使用Rails 3,其中'on'方法不再存在。我改用'get'方法[undefined method on](http://stackoverflow.com/questions/7526499/undefined-method-on-for-actionmodel) –
在你的答案中用'get'代替'on'它爲我工作。 –