客戶表中有活動的字段名稱。它如下文驗證在customer.rb:在rspec中被認爲是否爲false?
validates :active, :presence => true
這裏是RSpec的代碼來測試一個字段SHORT_NAME:
it "should be OK with duplicate short_name in different active status" do
customer = Factory(:customer, :active => false, :short_name => "test user")
customer1 = Factory.build(:customer, :active => true, :short_name => "Test user")
customer1.should be_valid
end
驗證爲SHORT_NAME是:
validates :short_name, :presence => true, :uniqueness => { :scope => :active }
上面的代碼的原因錯誤:
1) Customer data integrity should be OK with duplicate short_name in different active status
Failure/Error: customer = Factory(:customer, :active => false, :short_name => "test user")
ActiveRecord::RecordInvalid:
Validation failed: Active can't be blank
# ./spec/models/customer_spec.rb:62:in `block (3 levels) in <top (required)>'
似乎分配給字段活動的錯誤值在rspec中被認爲是空白或零,並且數據驗證檢查失敗。試圖使用0作爲錯誤,它會導致相同的錯誤。如果刪除對活動字段的驗證,rspec情況會通過。
你說得對。謝謝。 – user938363