未定义的方法`password_confirmation ='为零:NilClass

时间:2014-06-10 21:30:53

标签: ruby-on-rails testing rspec

在运行我的测试套装时出现此错误,并且它没有显示为失败的情况,而是系统上的完全错误。

这些是我的测试:

require 'spec_helper'

describe User do

  before do
    @user = User.new(name: "Example User", username: "ExampleUser", email: "user@example.com",
                     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[user@foo.COM A_US-ER@f.b.org frst.lst@foo.jp a+b@baz.cn]
      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) { "Foo@ExAMPle.CoM" }

    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: "user@example.com",
                     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

到目前为止,我的模型仅包含以下内容:

class User < ActiveRecord::Base
  has_secure_password
end

其他信息:

我注释了测试密码和password_confirmation的方法,但是离开了respond_to测试并且它们通过了。在查看剩余的失败测试时,会创建一个password_digest,因此has_secure_password正在运行

1 个答案:

答案 0 :(得分:1)

您需要将作业放在before块中。例如:

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

  it { should_not be_valid }
end

你也可以将它们放在it块中,但设置在before块中看起来更好。