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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With