2
votes

I'm writing a user model and RSpec is insisting I left fields blank that are, in fact, populated with a perfectly valid password. Here is my spec/models/user_spec.rb file:

require 'spec_helper'

describe User do
    before(:each) do
        @attr = {
            :name => "Example User",
            :email => "[email protected]",
            :password => "password",
            :password_confirmation => "password"
        }
    end

    it "should create a new instance given valid attributes" do
        User.create!(@attr)
    end

    it "should require a name" do
        no_name_user = User.new(@attr.merge(:name => ""))
        no_name_user.should_not be_valid
    end

    it "should reject names that are too long" do
        long_name = "a" * 51
        long_name_user = User.new(@attr.merge(:name => long_name))
        long_name_user.should_not be_valid
    end

    it "should accept valid email addresses" do
        addresses = %w[[email protected] [email protected] [email protected]]
        addresses.each do |address|
            valid_email_user = User.new(@attr.merge(:email => address))
            valid_email_user.should be_valid
        end
    end

    it "should reject invalid email addresses" do
        addresses = %w[user@foo,com user_at_foo.org example.user@foo.]
        addresses.each do |address|
            invalid_email_user = User.new(@attr.merge(:email => address))
            invalid_email_user.should_not be_valid
        end
    end

    it "should reject duplicate email addresses" do
        User.create!(@attr)
        user_with_duplicate_email = User.new(@attr)
        user_with_duplicate_email.should_not be_valid
    end

    it "should reject email addresses identical up to case" do
        upcased_email = @attr[:email].upcase
        User.create!(@attr.merge(:email => upcased_email))
        user_with_duplicate_email = User.new(@attr)
        user_with_duplicate_email.should_not be_valid
    end

    describe "password validations" do
        it "should require a password" do
            User.new(@attr.merge(:password => "", :password_confirmation => "")).should_not be_valid
        end

        it "should require a matching password confirmation" do
            User.new(@attr.merge(:password_confirmation => "invalid")).should_not be_valid
        end

        it "should reject short passwords" do
            short = "a" * 5
            hash = @attr.merge(:password => short, :password_confirmation => short)
            User.new(hash).should_not be_valid
        end

        it "should reject long passwords" do
            long = "a" * 41
            hash = @attr.merge(:password => long, :password_confirmation => long)
            User.new(hash).should_not be_valid
        end
    end

    describe "password encryption" do
        before(:each) do
            @user = User.create!(@attr.merge(:password => "foobar", :password_confirmation => "foobar"))
        end

        it "should have an encrypted password attribute" do
            @user.should respond_to(:encrypted_password)
        end
    end
end

Here is my app/models/user.rb file:

class User < ActiveRecord::Base
    attr_accessible :name, :email
    attr_accessor :password

    email_regex = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i

    validates(:name, :presence => true,
        :length => { :maximum => 50 })

    validates(:email, :presence => true,
        :format => { :with => email_regex },
        :uniqueness => { :case_sensitive => false })

    validates(:password, :presence => true,
        :confirmation => true,
        :length => { :within => 5..41 })
end

After running RSpec, I receive the following errors:

  1) User should create a new instance given valid attributes
     Failure/Error: User.create!(@attr)
     ActiveRecord::RecordInvalid:
       Validation failed: Password can't be blank
     # ./spec/models/user_spec.rb:25:in `block (2 levels) in <top (required)>'

  2) User should accept valid email addresses
     Failure/Error: valid_email_user.should be_valid
       expected #<User id: nil, name: "Example User", email: "[email protected]", created_at: nil, updated_at: nil, encrypted_password: nil> to be valid, but got errors: Password can't be blank
     # ./spec/models/user_spec.rb:43:in `block (3 levels) in <top (required)>'
     # ./spec/models/user_spec.rb:41:in `each'
     # ./spec/models/user_spec.rb:41:in `block (2 levels) in <top (required)>'

  3) User should reject duplicate email addresses
     Failure/Error: User.create!(@attr)
     ActiveRecord::RecordInvalid:
       Validation failed: Password can't be blank
     # ./spec/models/user_spec.rb:56:in `block (2 levels) in <top (required)>'

  4) User should reject email addresses identical up to case
     Failure/Error: User.create!(@attr.merge(:email => upcased_email))
     ActiveRecord::RecordInvalid:
       Validation failed: Password can't be blank
     # ./spec/models/user_spec.rb:63:in `block (2 levels) in <top (required)>'

  5) User password encryption should have an encrypted password attribute
     Failure/Error: @user = User.create!(@attr.merge(:password => "foobar", :password_confirmation => "foobar"))
     ActiveRecord::RecordInvalid:
       Validation failed: Password can't be blank
 # ./spec/models/user_spec.rb:92:in `block (3 levels) in <top (required)>'

The problem with every one of these is that the password field is not blank! It is populated with the word "password" - which falls well between the limits of 5 and 41. On some occasions I've merged it into the attributes of that very specific test.

Can anybody please explain why these tests are failing?

1
You can't mass-assign the password attribute, as you only have an attr_accessor for it, and not attr_accessible. You generally don't want the password to be attr_accessible anyways. - Amit Kumar Gupta
@AmitKumarGupta So how exactly would I change my user class to allow for testing of it? - Mark
@Mark Either attr_accessible :password or assign it manually. user = User.new...; user.password = "foo"; user.password_confirmation = "foo". - Chris Heald

1 Answers

7
votes

I don't know the exact issue, but I can teach you how to debug it.

Step 1: Open up a Rails console in the test environment.

$> rails console test

This allows you to execute code as if it were in a test spec. I am not sure if you are familiar with environments, but here is a good article anyway.

Step 2: Pick the easiest failure to fix. In this case, it appears to be User should create a new instance given valid attributes.

Step 3: Type in all the code you expect to be executed for the failing spec, line by line, into the test console.

>> @attr = {
        :name => "Example User",
        :email => "[email protected]",
        :password => "password",
        :password_confirmation => "password"
    }
>> User.create!(@attr)

Step 4: If you are unable to reproduce the failure, something else is wrong with your setup. Look inside spec_helper.rb. Maybe you forgot to run rake db:migrate or rake db:test:prepare? This becomes more important as you use more advanced tools like Zeus. Fix it.

Step 5: If you are able to reproduce the failure, yeah! Carefully inspect the error messages that are printed. As @AmitKumarGupta mentioned, it could be because password is not mass-assignable. Here is a good article on what that means. Try various ways of creating the user. For example,

>> user = User.new @attr
>> user.valid?  # should return true, but if it is false ...
>> user.errors  # is there an error for password and password confirmable?
>> user.inspect # maybe some rouge code deleted the password by accident?

Step 6: Hopefully by now you have found the solution. Add the solution and rerun the specs. Now repeat from Step 1 until it all passes.

Side Note

I strongly recommend FactoryGirl and Shoulda. Here is a gist for should have_a_valid_factory.

Update

If you followed my instructions above and executed the following

>> u = User.new @attr

You will see the following error message:

WARNING: Can't mass-assign protected attributes for User: password, password_confirmation

The article I linked above will explain what this means. Using FactoryGirl will fix your issue, or you can use

user = User.new @attr # without password, password confirmation
user.password = 'password'
user.password_confirmation = 'password'
user.save!