Search code examples
ruby-on-railsrspecruby-on-rails-5rspec-rails

rspec / testing after_action


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 ?


Solution

  • Try have_received:

    post  '/abcd/receive.json', params: params
    expect(MyModule).to have_received(:my_method)