2
我有一個用戶的工廠是這樣的:發現錯誤
FactoryGirl.define do
factory :user do
email "[email protected]"
password "123456789"
end
end
我試圖爲用戶創造一個規範:
describe User do
it "should create a user with password" do
user = FactoryGirl.create(:user, password: "secret")
user.password.should == "secret"
end
end
當我運行測試,我得到這樣的信息:
1) User should create a user with password
Failure/Error: user = FactoryGirl.create(:user, password: "secret")
ActiveRecord::RecordInvalid:
is invalid
# ./spec/models/user_spec.rb:3:in `block (2 levels) in <top (required)>'
# -e:1:in `<main>'
問題是:如何發現模型中哪些字段是無效的?換句話說,如何在模型中打印錯誤消息?
如果我沒有誤解你在說什麼,那麼建立記錄時就不可能得到錯誤,因爲構建不應觸發.save!在記錄上。如果你的記錄觸發.save!在創造新紀錄的時候,你可能會做出一些非常錯誤的事情。 –