如何检查我的对象是否有密钥?

时间:2014-08-11 09:36:28

标签: ruby-on-rails ruby rspec

我正在为我的登录方法执行三种不同的情况,在成功登录后,我收到以下auth令牌

{"auth":"eyJhbGciOiJIUzI1NiJ9.eyJlbWFpbCI6IkRpZGllci5EZXNjaGFtcHNAbm93aGVyZS5jb20iLCJoYXNoIjoiMFIzM3lnckhJNTNjNDg1NDA4YzliZWUzZDEyM2UwZTQwY2I3OTc3YWEiLCJuYW1lIjoiRGlkaWVyIiwidXNlcmlkIjoyLCJhY2NvdW50aWQiOjB9.884v-blMRHE-VgYOvuJIOwZHNhuaKuhprYO9xU6IYtE"}

如何检查user object has the auth keyrspec是否通过测试。

require 'spec_helper'

describe User do

  context "when signing in" do

    it "should not login with invalid code" do       
      user = User.login(email: email, password: password, confirmpassword: password, code: 10000);
    end

    it "should not login with no code" do 
      user = User.login(email: email, password: password, confirmpassword: password, code: "");
    end

    it "should login with valid code" do 
      user = User.login(email: email, password: password, confirmpassword: password, code: 0);
    end

  end

end

2 个答案:

答案 0 :(得分:1)

这应该有效:

expect(user.auth).not_to be_nil

VS

expect(user.auth).to be_nil

答案 1 :(得分:1)

我认为像

这样的东西
expect(user).to have_key(:auth)

expect(user).to_not have_key(:auth)

可以胜任。

检查docs是否有更多匹配器。