Reputation: 266960
My model has validation like:
# Account Model
validates :email, presence: true
validates :password, presence: true
So I created a factory_girl for this model that has blank values like:
factory :account do
end
trait :empty do
email ""
password ""
end
So my test looks like:
it "doesn't allow empty fields" do
account = create(:account, :empty)
account.should_not be_valid
end
So I am saying that this model, with empty fields, should fail the validation with "should_not".
Yet when I run rspec I get this error:
Validation failed: Email can't be blank, Password can't be blank
# ./spec/models/account_spec.rb:29:in `block (3 levels) in <top (required)>'
What is the problem with my test?
Upvotes: 0
Views: 250
Reputation: 13181
The rspec error comes from this line account = create(:account, :empty)
, not this one account.should_not be_valid
create
will attempt to create the record in the database, and go through the validation. use build
instead
Upvotes: 4