Ruby on rails 3 ruby教程测试失败(Michael Hartl)

Ruby on rails 3 ruby教程测试失败(Michael Hartl),ruby-on-rails-3,rspec,Ruby On Rails 3,Rspec,我在学习迈克尔·哈特尔的教程。我正在使用RSPEC运行测试。 到目前为止还不错,但我似乎已经用下面的例子击中了墙 以下是失败的测试(应该通过): 以防万一。 @用户定义为: @attr定义为: user.rb中的条目 测试失败时显示的错误消息 c:\RailsInstaller\work\apptwit>rspec spec/models/user\u spec.rb F 失败: 1) 用户密码验证密码加密验证方法应在电子邮件/密码中返回用户 失败/错误:匹配_user.should==@use

我在学习迈克尔·哈特尔的教程。我正在使用RSPEC运行测试。 到目前为止还不错,但我似乎已经用下面的例子击中了墙

以下是失败的测试(应该通过):

以防万一。 @用户定义为:

@attr定义为:

user.rb中的条目

测试失败时显示的错误消息

c:\RailsInstaller\work\apptwit>rspec spec/models/user\u spec.rb
F
失败:
1) 用户密码验证密码加密验证方法应在电子邮件/密码中返回用户
失败/错误:匹配_user.should==@user

应为:#如果
匹配用户
为零,则您可能希望放置一些
放置电子邮件
放置用户。检查
self.authenticate
中的
语句以调试它


似乎无法通过电子邮件找到用户,或者由于身份验证方法中的某些原因,您的密码不正确。

如果
匹配用户
为零,则您可能需要放置一些
放置电子邮件
放置用户。检查
self.authenticate
中的
语句以调试它


似乎无法通过电子邮件找到用户,或者由于身份验证方法中的某些原因,您的密码不正确。

是否有任何方法可以为注释添加点数?(新到这个网站)没有,但我可以添加它作为一个答案,如果它有助于解决你的问题,然后你可以。我很好奇到底发生了什么,什么是固定的,它不是固定的,但我发现这些想法很有用-我正在使用Hart tutorialNo学习RoR,但是它会使您的密码不正确,因为
has\u password?
会加密您传入的字符串,以检查加密的密码字段是否与您传入的
has\u password?
匹配。如果salt开始时为nil,然后稍后获得一个值,
has\u password?
将返回false。而且,零盐是不安全的,所以无论哪种方式都需要修正。好的,我已经解决了。我在users.rb中有一行“attr_accessor:password,:salt”,它基本上使:salt成为一个虚拟属性。但是数据库中有一个对应的列,它实际存储salt散列。(我不得不按照SO中另一篇帖子的建议在这行中加盐,但我不应该这样做)。一旦你从attr_accessor:password中删除:salt,:salt,那么一切都开始工作了。有什么方法可以为注释添加点吗?(新到这个网站)没有,但我可以添加它作为一个答案,如果它有助于解决你的问题,然后你可以。我很好奇到底发生了什么,什么是固定的,它不是固定的,但我发现这些想法很有用-我正在使用Hart tutorialNo学习RoR,但是它会使您的密码不正确,因为
has\u password?
会加密您传入的字符串,以检查加密的密码字段是否与您传入的
has\u password?
匹配。如果salt开始时为nil,然后稍后获得一个值,
has\u password?
将返回false。而且,零盐是不安全的,所以无论哪种方式都需要修正。好的,我已经解决了。我在users.rb中有一行“attr_accessor:password,:salt”,它基本上使:salt成为一个虚拟属性。但是数据库中有一个对应的列,它实际存储salt散列。(我不得不按照SO中另一篇帖子的建议在这行中加盐,但我不应该这样做)。一旦从attr_accessor:password中删除:salt,:salt,那么一切都开始工作。
describe "authenticate method" do
  it "should return the user on email/password match" do
    matching_user = User.authenticate(@attr[:email], @attr[:password])
    matching_user.should == @user
  end
end
before(:each) do
  @user = User.create!(@attr)
end
before(:each) do        
  @attr = {
          :name => "Example user", 
          :email => "user@example.com",
          :password => "foobar",
          :password_confirmation => "foobar"
        }
end
before_save :encrypt_password

def has_password?(submitted_password)
    encrypted_password == encrypt(submitted_password)
end

def self.authenticate(email, submitted_password)
    user = find_by_email(email)
    return nil  if user.nil?
    return user if user.has_password?(submitted_password)
end

private

def encrypt_password 
      self.salt = make_salt unless has_password?(password)
      self.encrypted_password = encrypt(password) 
end

def encrypt(string)
      secure_hash("#{salt}--#{string}")
end

def make_salt
      secure_hash("#{Time.now.utc}--#{password}")
end

def secure_hash(string)
      Digest::SHA2.hexdigest(string)
end
c:\RailsInstaller\work\apptwit>rspec spec/models/user_spec.rb
.................F

Failures:

  1) User password validations password encryption authenticate method should return the user on email/password
     Failure/Error: matching_user.should == @user
       expected: #<User id: 1, name: "Example user", email: "user@example.com", created_at: "2011-12-07 19:08:23
ed_at: "2011-12-07 19:08:23", encrypted_password: "fbdbaf712fa1b6c925c4ab2192e73ac9f9d1bedf67630610d68...">
            got: nil (using ==)
     # ./spec/models/user_spec.rb:204:in `block (5 levels) in <top (required)>'

Finished in 221.37 seconds
18 examples, 1 failure

Failed examples:

rspec ./spec/models/user_spec.rb:202 # User password validations password encryption authenticate method should
he user on email/password match