ShEsKo
ShEsKo

Reputation: 157

Shoulda-matchers test with FactoryGirl, password_confirmation not set?

I am testing my account model with shoulda-matchers using a entity created with FactoryGirl.

The code of both files look like this:

TEST FILE

require 'spec_helper'

describe Account do
  before { @account = FactoryGirl.build(:account) }
  subject { @account }

  it { should validate_presence_of(:email) }
  it { should validate_presence_of(:password) }
  it { should validate_presence_of(:password_confirmation).
        with_message(@account.password_confirmation+' '[email protected]) }
  it { should allow_value('[email protected]').for(:email) }

  it { should be_valid }
end

FACTORY

FactoryGirl.define do
  factory :account do
    email { FFaker::Internet.email }
    password "12345678"
    password_confirmation "12345678"
  end

end

My error is the following:

1) Account should require password_confirmation to be set
  Failure/Error: it { should validate_presence_of(:password_confirmation).
   Expected errors to include "12345678 12345678" when password_confirmation is set to nil, got errors: ["password_confirmation can't be blank (nil)"]
 # ./spec/models/account_spec.rb:9:in `block (2 levels) in <top (required)>'


rspec ./spec/models/account_spec.rb:9 # Account should require password_confirmation to be set

I am using devise which should check for password confirmation. I know its probably because of something stupid, but I really can't figure out what's wrong with it.

Upvotes: 0

Views: 315

Answers (1)

Leonel Gal&#225;n
Leonel Gal&#225;n

Reputation: 7167

Devise, doesn't validate the presence of password_confirmation it just validates the confirmation of password see Validatable line 34: https://github.com/plataformatec/devise/blob/v3.5.3/lib/devise/models/validatable.rb#L34

validates_confirmation_of :password, if: :password_required?

EDIT: You could also see that there are no validators on password_confirmation running: User.validators_on(:password_confirmation)

Upvotes: 1

Related Questions