0

I am attempting to test my User model (using the devise gem). I'm running on the rails4 branch of the devise gem. And I'm trying to write a test for the minimum password length.

In my user_spec.rb, I've written:

require 'spec_helper'

describe User do
  before { @user = User.new(full_name: "Example User", email: "[email protected]", password: "foobar", password_confirmation: "foobar") }

  subject { @user }

  it { should respond_to(:full_name) }
  it { should respond_to(:email) }
  it { should respond_to(:password) }
  it { should respond_to(:password_confirmation) }
  # it { should ensure_length_of(:password).is_at_least(8) }

  it { should be_valid }

  describe 'when full name is not present' do
    before { @user.full_name = " " }
    it { should_not be_valid }
  end

  describe 'when email is not present' do
    before { @user.email = " " }
    it { should_not be_valid }
  end

  describe 'when password is not present' do
    before {@user.password = " "}
    it { should_not be_valid }
  end

  describe 'when password is too short' do
    it { should ensure_length_of(:password).is_at_least(8) }
    it { should_not be_valid }
  end
end

However, I'm still getting this failure/error when running rspec spec/:

Failure/Error: it { should be_valid }
expected #<User id: nil, email: "[email protected]", encrypted_password:
"$2a$04$/Ifwb1dmfzG6xtBS/amRfOrTTopd8P6JSV48L0G/SWSh...",
reset_password_token: nil, reset_password_sent_at: nil,
remember_created_at: nil, sign_in_count: 0, current_sign_in_at: nil,
last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil,
created_at: nil, updated_at: nil, full_name: "Example User"> to be valid,
but got errors: Password is too short (minimum is 8 characters)
# ./spec/models/user_spec.rb:14:in `block (2 levels) in <top (required)>'
1
  • foobar is 6 characters long Commented Apr 29, 2013 at 12:41

2 Answers 2

1

As I see it, your spec file is working just fine.

Your it { should be_valid } test is failing on Line 14. This is failing because you have password as "foobar" which is just 6 characters long, and thus makes the user invalid.

Try changing

before do  
  @user = User.new(
    full_name: "Example User", 
    email: "[email protected]", 
    password: "foobar", 
    password_confirmation: "foobar")
end

to:

before do  
  @user = User.new(
    full_name: "Example User", 
    email: "[email protected]", 
    password: "foobar123", 
    password_confirmation: "foobar123")
end
Sign up to request clarification or add additional context in comments.

Comments

1

The User you created for your test has not a valid password. So your test is actually guaranteeing the intended behavior.

Change your Test User password to something like: "long test password" and it should work alright.

Best regards

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.