I am trying to test my Smser class in a rails application, and trying to use mocha to stub the actual sending method only. I should mention also that this is my first attempt at mocking/stubbing.
When running the test, I get the failure unexpected invocation: :remove_method - which looks like what mocha called on my object, to stub the method. So I am not sure what to do.
Here are the relevant code snippets:
Smser class
# models/smser.rb
class Smser
...<truncated>...
def self.instance
@@instance ||= self.new
end
private
# Need to stub this
def twilio(args)
@client.account.messages.create args
end
end
Test
# test/models/smser.rb
test "should send sms" do
Smser.instance.stubs(:twilio).with(from: '123', to: '234', body: 'Message body')
# <- ... here we run something that should invoke 'twilio' on the Smser object
end
Test Output
unexpected invocation:
#<Smser:0x68e4940>.twilio(:from => '123', :to => :remove_method, :body => :twilio)
satisfied expectations:
- allowed any number of times, invoked once:
#<Smser:0x68e4940>.twilio(:from => '123', :to => '234', :body => 'Message body')
Any help is appreciated.
The error practically means that Mocha is expecting different invocation of the twilio method on the Smser class.
Instead of:
Smser.instance.stubs(:twilio).with(from: '123', to: '234', body: 'Message body')
it should be:
Smser.instance.stubs(:twilio).with(from: '123', to: '234', body: :twilio)
because, as the error states, the body isn't "Message body" but :twilio.
HTH
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