Please make sure you have password_digest exist in your User schema, details please see here
create_table “users”, force: true do |t|
……
t.text “password_digest”
…..
end
If it works in your development but not test, please check if you do the rake db:migration in test env, I mean RAILS_ENV=test rake db:migrate.
!!!Updated -- related to shouda-matchers
gocha
It seems a bug for shoulda-matchers
, cause when you do this the test will pass:
it "should be validate uniq of email " do
user.save
expect(user).to validate_uniqueness_of :email
end
the reason why I am doing the user.save
is because of this
, otherwise it will create a record for you, that will cause the error you got:
# https://github.com/thoughtbot/shoulda-matchers/blob/master/lib/shoulda/matchers/active_model/validate_uniqueness_of_matcher.rb#L126
def create_record_in_database(options = {})
if options[:nil_value]
value = nil
else
value = "arbitrary_string"
end
@subject.class.new.tap do |instance|
instance.send("#{@attribute}=", value)
instance.save(:validate => false) # the error happens here, not in your code
end
end
So above all, this is probably a bug of shouda-matchers
, though I didn't have time to figure out how to fix it, for now you can use this method as a work around, or use some other method to test this.
Hope it helps :-)