Question

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?

Was it helpful?

Solution

You can always temporarily change your test to:

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

and then check user.valid? and user.errors

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top