Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Failure/Error: expected: 1 time with any arguement, recieved: 0 times with any arguments

I have a below spec, where i am mocking my user model and stubbing its method.

require 'spec_helper'

describe User do 

  let(:username) {"[email protected]"}
  let(:password) {"123"}
  let(:code) {"0"}

  context "when signing in" do
    let(:expected_results) { {token:"123"}.to_json }

    it "should sign in" do 
      expect(User).to receive(:login).with({email: username, password: password, code: code})
        .and_return(expected_results)
    end

  end    
end

I get the below error, when i try to run my test case.

Failure/Error: expect(User).to receive(:login).with({email: username, password: password, code: code})
       (<User (class)>).login({:email=>"[email protected]", :password=>"123", :code=>"0"})
           expected: 1 time with arguments: ({:email=>"[email protected]", :password=>"123", :code=>"0"})
           received: 0 times
like image 878
UnderTaker Avatar asked Feb 12 '23 06:02

UnderTaker


1 Answers

You are misunderstanding what expect is.

expect(x).to receive(:y) is stubbing out the y method on the x.

ie, it is papering over that method.

A way of describing this would be that you are making an "expectation that method y will be called on x when you actually run your code"

Right now, you spec doesn't call any actual code... it just sets up the expectation... then stops.

If you are testing the method login then you need to not stub it out with an expectation, but actually call it for real.

eg User.login(email: username, password: password, code: code)

You currently don't actually have a test at all. Just a stub that you set up, and then never use.

like image 51
Taryn East Avatar answered Feb 15 '23 10:02

Taryn East