8vius
8vius

Reputation: 5836

Undefined method `password_confirmation=' for nil:NilClass

Getting this error when running my test suit, and it's not showing as a failing case but outright error on the system.

These are my tests:

require 'spec_helper'

describe User do

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

  subject { @user } 

  it { should respond_to(:name) }
  it { should respond_to(:username) }
  it { should respond_to(:website) }
  it { should respond_to(:bio) }
  it { should respond_to(:profile_path) }
  it { should respond_to(:cover_path) }

  it { should respond_to(:password_digest) }
  it { should respond_to(:password) }
  it { should respond_to(:password_confirmation) }
  it { should respond_to(:authenticate) }
  it { should respond_to(:venue_user) }
  it { should respond_to(:admin) }

  it { should respond_to(:twitter_token) }
  it { should respond_to(:facebook_token) }
  it { should respond_to(:facebook_token) }
  it { should respond_to(:google_token) }

  it { should be_valid }
  it { should_not be_admin }

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

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

  describe "when username is too long" do
    before { @user.username = 'a' * 16}
    it { should_not be_valid }
  end

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

  describe "when email format is invalid" do
    it "should be invalid" do
      addresses = %w[user@foo,com user_at_foo.org example.user@foo.
                     foo@bar_baz.com foo@bar+baz.com]
     addresses.each do |invalid_address|
       @user.email = invalid_address
       expect(@user).not_to be_valid
     end
    end
  end

  describe "when email format is valid" do
    it "should be valid" do
      addresses = %w[[email protected] [email protected] [email protected] [email protected]]
      addresses.each do |valid_address|
        @user.email = valid_address
        expect(@user).to be_valid
      end
    end
  end

  describe "when email address is already taken" do
    before do
      user_with_same_email = @user.dup
      user_with_same_email.email = @user.email.upcase
      user_with_same_email.save
    end

    it { should_not be_valid }
  end

  describe "email address with mixed case" do 
    let(:mixed_case_email) { "[email protected]" }

    it "should be saved as lower case" do
      @user.email = mixed_case_email
      @user.save
      expect(@user.reload.email).to eq mixed_case_email.downcase
    end
  end

  describe "when password is not present" do
    @user = User.new(name: "Example User", username: "ExampleUser", email: "[email protected]",
                     password: " ", password_confirmation: " ")
    it { should_not be_valid }
  end

  describe "when password and confirmation do not match" do
    @user.password_confirmation = "mismatch"
    it { should_not be_valid }
  end

  describe "with a password that's too short" do
    @user.password = @user.password_confirmation = 'a' * 5
  end

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

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

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

      it { should_not eq user_for_invalid_password } 
      specify { expect(user_for_invalid_password).to be_false } 
    end
  end

  describe "remember token do" do
    before { @user.save }
    its(:remember_token) { should_not be_blank } 
  end 
end

My model only contains the following so far:

class User < ActiveRecord::Base
  has_secure_password
end

Additional info:

I commented out the methods that test the password and password_confirmation, but left the respond_to tests and they pass. And viewing the remaining failing tests there is a password_digest created, so the has_secure_password is working

Upvotes: 0

Views: 408

Answers (1)

Douglas F Shearer
Douglas F Shearer

Reputation: 26518

You need to put your assignments in a before block. For example:

describe "when password and confirmation do not match" do
  before do
    @user.password_confirmation = "mismatch"
  end

  it { should_not be_valid }
end

You could also put these in the it block, but the setup looks nicer in a before block.

Upvotes: 1

Related Questions