Reputation: 5182
I guess am not testing it the right way, but still I was wondering how to make sure a method call is triggered in an after_action.
Having the followings :
class AbcdController < ApplicationController
after_action :handle_after, only: [:receive]
def receive
…… do some stuff
end
private
def handle_after
MyModule.my_method(some_data)
end
end
Then I'd like to test it like so (in a request spec) :
post '/abcd/receive.json', params: params
expect(MyModule).to receive(:my_method)
This test does not passes. What would be an other approach to make sure the MyModule.method
is called when the url if reached ?
Upvotes: 2
Views: 1976
Reputation: 36
Try have_received
:
post '/abcd/receive.json', params: params
expect(MyModule).to have_received(:my_method)
Upvotes: 2
Reputation: 6648
That's basically the way to do it.
Maybe, when you want more integration approach, instead of checking if MyModule
received :my_method
test the effect of calling this Module.
For example, if it was recording an event in the DB, check
expect {post '/abcd/receive.json'}.to change{ Event.count }.by(1)
You need to define borders when your testing stops, so you have to balance amount of mocking with deeply checking the behavior of your app.
Upvotes: 0