Ruby Hartl教程第7章中的故障问题

Ruby Hartl教程第7章中的故障问题,ruby,testing,encryption,rspec,Ruby,Testing,Encryption,Rspec,当我运行bundle exec rspec spec/时,我得到了21个示例和3个失败。这些失败是: 失败: 1) 用户有密码吗?如果密码匹配,则方法应为true 失败/错误:@user.has_password?(@attr[:password])。应为\u true 命名错误: 未定义的方法具有nil:NilClass的\u密码 #./spec/models/user_spec.rb:47:in块(3层)in' 2) 用户有密码吗?如果密码不匹配,则方法应为false 失败/错误:@user

当我运行bundle exec rspec spec/时,我得到了21个示例和3个失败。这些失败是:

失败:

1) 用户有密码吗?如果密码匹配,则方法应为true 失败/错误:@user.has_password?(@attr[:password])。应为\u true 命名错误: 未定义的方法
具有nil:NilClass的\u密码
#./spec/models/user_spec.rb:47:in
块(3层)in'

2) 用户有密码吗?如果密码不匹配,则方法应为false 失败/错误:@user.has\u密码?(“无效”)。应为\u false 命名错误: 未定义的方法
具有nil:NilClass的\u密码
#./spec/models/user_spec.rb:51:in
块(3级)in'

3) 用户密码验证应接受有效的电子邮件地址 失败/错误:它“应该拒绝无效的电子邮件地址”吗 命名错误: 未定义的方法
it'用于#
#./spec/models/user_spec.rb:97:in
块(3层)in'

我将发布我的user_spec.rb文件bc,我认为这几乎是正确的,但并不完全正确。请注意注释掉的结尾,我以前也有过这些结尾,但我认为它们是错误的,所以注释掉了它们

require 'spec_helper'

describe User do

  before(:each) do
    @attr = { 
      :name => "Example User", 
      :email => "user@example.com", 
      :password => "foobar", 
      :password_confirmation => "foobar" }
  end

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

  describe "password encryption" do

    before(:each) do
      @user = User.create!(@attr)
  end

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

    it "should set the encrypted password" do
      @user.encrypted_password.should_not be_blank
  end
end

  describe "has_password? method" do

    it "should be true if the passwords match" do
      @user.has_password?(@attr[:password]).should be_true
  end

    it "should be false if the passwords don't match" do
      @user.has_password?("invalid").should be_false
  end
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
    short = "a" * 5
    hash = @attr.merge(:password => short, :password_confirmation => short)
    User.new(hash).should_not be_valid
  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 require an email address" do
    no_email_user = User.new(@attr.merge(:email => ""))
    no_email_user.should_not be_valid
  end

  it "should accept valid email addresses" do
    addresses = %w[user@foo.com THE_USER@foo.bar.org first.last@foo.jp]
    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
    # Put a user with given email address into the database.
    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

  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
      end
    end
  end
end
我想我的user.rb文件很好

所以3次失败是我问题的一个方面,但真正让我担心的是以下命令:

bundle exec rspec spec/models/user_spec.rb-e“has_password\?method”

终端中的结果如下:

没有匹配的示例{:full_description=>/(?-mix:has_password\\?\method)/}

以0.00003秒完成 0个示例,0个失败


根据Hartl,我应该有2个例子,0个失败。思想?感谢您的任何输入:)

在您的user_spec.rb文件中

确保您有:

       before(:each) do
         @user = User.create!(@attr)
       end
在下一行之后:

     describe "has_password? method" do
教程中的代码中缺少它。您将看到它是密码加密块的一部分。它看起来像是为该测试删除了一个用户。这不是很干燥…可能是一种让存根块为每个描述块运行的方法,但这比我要远一点。:)希望它能帮上忙…让我的测试顺利进行