문제

I have a user factory like this:

FactoryGirl.define do
  factory :user do
    email "email@x.com"
    password "123456789"
  end
end

I am trying to create a spec for the user:

describe User do
  it "should create a user with password" do
    user = FactoryGirl.create(:user, password: "secret")
    user.password.should == "secret"
  end   
end

When I run the test, I get this message:

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

The problem is: how to discover what are the fields in the model that are invalid? In other words, how to print the error messages inside the model?

도움이 되었습니까?

해결책

You can always temporarily change your test to:

user = FactoryGirl.build(:user, password: "secret")

and then check user.valid? and user.errors

다른 팁

Sometimes the build method gives you the same ActiveRecord::RecordInvalid: Record invalid. In that case you might want to look for any associated records that your factory creates, maybe the validation problem comes from those instead.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top