Rspec身份验证密码测试无故失败

时间:2014-07-04 13:57:20

标签: ruby-on-rails ruby-on-rails-4 rspec mongomapper

我正在使用rails和mongoDB(mongomapper)学习ruby,我跟着Michael Hartl的教程,遇到了一个问题。问题是测试密码验证的某个地方。当我使用控制台并尝试身份验证时,它就像一个魅力,但在运行测试时,相同的身份验证测试通过了几次,然后突然失败告诉我db和变量中的密码不匹配。以下是测试代码和测试消息。

规格/模型/ special_case.rb

require 'spec_helper'
require 'pry-debugger'

describe User do
  before do
      @user =  build(:user)
  end

  subject { @user }

  it { should respond_to(:authenticate) }

  it { should be_valid }

  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 eq 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
end

这是错误代码

bojan@bojan-300E4C-300E5C-300E7C:~/rubyprojects/sample_app$ bundle exec rspec spec/special_case.rb 
[DEPRECATION] :index option when defining key "email" is deprecated. Put indexes in `db/indexes.rb`
....F

Failures:

  1) User return value of authenticate method with valid password 
     Failure/Error: it { should eq found_user.authenticate(@user.password) }

   expected: #<User _id: BSON::ObjectId('53b6b0f6ac04d82c8e000003'), created_at: Fri, 04 Jul 2014 13:49:42 UTC +00:00, email: "boja00@yahoo.com", name: "Bojan Drljaca", password_digest: "$2a$04$Os57kzr4a/1.5Sf5KG5Ib.MV8xThCY0Tok8SiRVC/9b6vK8kiRFTi", updated_at: Fri, 04 Jul 2014 13:49:42 UTC +00:00>
        got: #<User _id: BSON::ObjectId('53b6b0f6ac04d82c8e000005'), email: "boja00@yahoo.com", name: "Bojan Drljaca", password_digest: "$2a$04$WGbfIKjDGvK/0nGPaAZXyuiH/z86MVIe0r6TWlQ72mIQw4XmRuR1m">

   (compared using ==)

   Diff:
   @@ -1,2 +1,2 @@
   -#<User _id: BSON::ObjectId('53b6b0f6ac04d82c8e000003'), created_at: Fri, 04 Jul 2014 13:49:42 UTC +00:00, email: "boja00@yahoo.com", name: "Bojan Drljaca", password_digest: "$2a$04$Os57kzr4a/1.5Sf5KG5Ib.MV8xThCY0Tok8SiRVC/9b6vK8kiRFTi", updated_at: Fri, 04 Jul 2014 13:49:42 UTC +00:00>
   +#<User _id: BSON::ObjectId('53b6b0f6ac04d82c8e000005'), email: "boja00@yahoo.com", name: "Bojan Drljaca", password_digest: "$2a$04$WGbfIKjDGvK/0nGPaAZXyuiH/z86MVIe0r6TWlQ72mIQw4XmRuR1m">

 # ./spec/special_case.rb:20:in `block (4 levels) in <top (required)>'

Finished in 0.10159 seconds
5 examples, 1 failure

Failed examples:

rspec ./spec/special_case.rb:20 # User return value of authenticate method with valid password 

Randomized with seed 5909

测试有时通过,突然失败并告诉我密码不同(看哈希)。

来自调试器的

代码

[1] sample_app(#<RSpec::Core::ExampleGroup::Nested_1::Nested_1::Nested_1>) »  found_user
=> #<User:0x00000007cc18c8> {
            "_id" => BSON::ObjectId('53b6b7c1ac04d82e9b000003'),
     "created_at" => Fri, 04 Jul 2014 14:18:41 UTC +00:00,
          "email" => "boja00@yahoo.com",
           "name" => "Bojan Drljaca",
"password_digest" => "$2a$04$wg5lDkRFv2u4P90FZ.5EGesNedNr2IifDMiVT/iEU222llk.c8eea",
"updated_at" => Fri, 04 Jul 2014 14:18:41 UTC +00:00
}

[2] sample_app(#<RSpec::Core::ExampleGroup::Nested_1::Nested_1::Nested_1>) »  @user
=> #<User:0x0000000718f040> {
            "_id" => BSON::ObjectId('53b6b7e3ac04d82ea6000005'),
     "created_at" => nil,
          "email" => "boja00@yahoo.com",
           "name" => "Bojan Drljaca",
"password_digest" => "$2a$04$63/jTwT680HDjlQrUP/dVeTwtHb9.PRygGUTj9LoHypEuVesfEm3S",
     "updated_at" => nil
}

1 个答案:

答案 0 :(得分:0)

问题与密码无关。预期用户具有created_atupdated_at属性,而实际用户则没有。我想你的found_user永远不会被保存?

另一种可能性是直接测试属性而不是整个用户:

describe "with valid password" do
  let(:result) {found_user.authenticate(@user.password)}

  it 'should fetch the user' do  
    @user.email.should eq result.email
    @user.name.should  eq result.name
  end
end