johnnycakes
johnnycakes

Reputation: 2450

RSpec / Rails - How do I test that a method in ApplicationController is called (when I'm testing a subclassed controller)?

Title says it all.

I have a controller action in which I do some stuff, then call a method that is defined in ApplicationController.

How do I test that it was called?

controller.should_receive(:the_method_name) doesn't work.
ApplicationController.should_receive(:the_method_name) doesn't work.

What's the proper syntax?

Thanks.

Upvotes: 3

Views: 2389

Answers (2)

moreinput
moreinput

Reputation: 31

Using the AnonymousController as Gerep suggested and calling controller.should_receive(:the_method_name) should do it.

The position within the test is important to let the test pass successfully. To give you an example:

describe 'my test' do
  before do
    controller.should_receive(:the_method)
    get :action
  end
  it { should render_template(:your_template) }
end

This example should pass and tests also the call of :the_method.

Upvotes: 0

user745235
user745235

Reputation:

I think you can use anonymous controller to test your ApplicationController, check the documentation.

Upvotes: 1

Related Questions