1

I am executing three different cases for my login method, on successful login i receive the below auth token

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

How do i check whether my user object has the auth key or not in rspec to pass the test.

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
UnderTaker
  • 853
  • 3
  • 13
  • 22

2 Answers2

1

This should work:

expect(user.auth).not_to be_nil

vs

expect(user.auth).to be_nil
BroiSatse
  • 44,031
  • 8
  • 61
  • 86
1

I think that something like

expect(user).to have_key(:auth)

and

expect(user).to_not have_key(:auth)

can do the job.

check the docs for more matchers.

xlembouras
  • 8,215
  • 4
  • 33
  • 42
  • `have_key` is reserved for hashes. Unfortunately, even though ActiveRecord defines `[]` method, ti is not a hash and does not respond to `has_key` method, hence the above won't work. – BroiSatse Aug 11 '14 at 09:53