Mike Simmons
Mike Simmons

Reputation: 328

Failing rspec test -- object is not being created

I have the following test code:

require 'spec_helper'
require 'matchers/be_valid_verbose'

describe User do

before do 
    @user = User.new(first_name: "First", last_name: "Last", email: "[email protected]", role: "admin",
                    password: "foobar12", password_confirmation: "foobar12")
end
subject ( @user )

specify { should be_valid_verbose }

describe "return value of authendicate method" do
    before { @user.save }
    let(:found_user) { User.find_by_email(@user.email) }

    describe "with valid password" do
        it { should == found_user.authenticate(@user.password) }
    end

        describe "with invalid password" do
            let(:user_for_invalid_password) { found_user.authenticate("invlaid") }

            it { should_not == user_for_invalid_password }
            specify { user_for_invalid_password.should be_false }
    end
end

end

I don't understand why: it { should be_valid } is gettig a totally empty user object. The same is happening when I try to find_by_email after just creating the user object in the before do statement. Here's my test output

    $ bundle exec rspec spec/models/user_trial_spec.rb
FF..

Failures:

  1) User 
     Failure/Error: specify { should be_valid_verbose }
       expected valid? to return true, got false:
        Password digest can't be blank
        First name can't be blank
        Last name can't be blank
        Role is not included in the list
        Email can't be blank
        Email is invalid
        Password can't be blank
        Password is too short (minimum is 8 characters)
        Password confirmation can't be blank
     # ./spec/models/user_trial_spec.rb:25:in `block (2 levels) in <top (required)>'

  2) User return value of authendicate method with valid password 
     Failure/Error: it { should == found_user.authenticate(@user.password) }
       expected: #<User id: 10, first_name: "First", last_name: "Last", email: "[email protected]", role: "admin", password_digest: "$2a$10$Z0c6zJNH4yu8IpYfNqEbKOmqEWK.euTFcYuwB/8UW9jk...", created_at: "2012-11-13 21:44:55", updated_at: "2012-11-13 21:44:55", remember_token: nil>
            got: #<User id: nil, first_name: nil, last_name: nil, email: nil, role: nil, password_digest: nil, created_at: nil, updated_at: nil, remember_token: nil> (using ==)
       Diff:
       @@ -1,2 +1,2 @@
       -#<User id: 10, first_name: "First", last_name: "Last", email: "[email protected]", role: "admin", password_digest: "$2a$10$Z0c6zJNH4yu8IpYfNqEbKOmqEWK.euTFcYuwB/8UW9jk...", created_at: "2012-11-13 21:44:55", updated_at: "2012-11-13 21:44:55", remember_token: nil>
       +#<User id: nil, first_name: nil, last_name: nil, email: nil, role: nil, password_digest: nil, created_at: nil, updated_at: nil, remember_token: nil>
     # ./spec/models/user_trial_spec.rb:32:in `block (4 levels) in <top (required)>'

Finished in 2.3 seconds
4 examples, 2 failures

Failed examples:

rspec ./spec/models/user_trial_spec.rb:25 # User 
rspec ./spec/models/user_trial_spec.rb:32 # User return value of authendicate method with valid password 

Randomized with seed 39450

Upvotes: 0

Views: 548

Answers (1)

Mike Simmons
Mike Simmons

Reputation: 328

Okay i just saw the problem. I was using

subject ( @user )

instead of the correct

subject { @user }

DOH!

Upvotes: 1

Related Questions