48

In my ApplicationController I have a method defined as a helper method:

helper_method :some_method_here

  • How do I test ApplicationController in RSpec at all?
  • How do I include/call this helper method when testing my views/helpers?

I'm using Rails3 with RSpec2

Mirko
  • 5,207
  • 2
  • 37
  • 33

2 Answers2

59

You can use an anonymous controller to test your ApplicationController, as describe in the RSpec documentation. There's also a section on testing helpers.

Paweł Gościcki
  • 9,066
  • 5
  • 70
  • 81
Jimmy
  • 35,686
  • 13
  • 80
  • 98
  • Somehow I missed that anonymous controller! Thanks! – Mirko Jan 19 '11 at 19:38
  • Exactly what I needed to know! – Matthew Nov 02 '11 at 18:13
  • 2
    is it still working in Rails4? I got problem with `No route matches {:controller=>"anonymous", :action => "some action"}`.. – chrmod Aug 06 '13 at 14:32
  • Possibly this issue: https://github.com/rspec/rspec-rails/issues/636. The anonymous controller only has routes for the default resource methods, so rename 'some action' to (say) 'index', or create a route. – Andy Triggs Apr 30 '15 at 09:48
  • I too wanted to make routes for each method I wanted to test but alas I could not. So I just called every method I wanted to test in the declaration of index and stubbed the non relevant ones out for each separate test using `allow(controller).to receive(:any_non_relevant_method_name).and_return(true)` I did this in a DRY way. – ryan2johnson9 Dec 07 '16 at 04:23
23

You can invoke your helper methods on subject or @controller in the specification.

I have been looking for a solution to this problem and anonymous controller was not what I was looking for. Let's say you have a controller living at app/controllers/application_controller.rb with a simple method which is not bound to a REST path:

class ApplicationController < ActionController:Base

  def your_helper_method
    return 'a_helpful_string'
  end

end

Then you can write your test in spec/controllers/application_controller_spec.rb as follows:

require 'spec_helper'

describe ApplicationController do

  describe "#your_helper_method" do
    it "returns a helpful string" do
      expect(subject.your_helper_method).to eq("a_helpful_string")
    end
  end

end

While @controller and subject can be used interchangeable here, I would go for subject as its the RSpec idiomatic way for now.

Konrad Reiche
  • 27,743
  • 15
  • 106
  • 143
  • 1
    Using your answer to test ```current_user``` helper, and it's working gracefully – Nícolas Iensen Jul 30 '14 at 15:19
  • This pattern will allow passing specs even for methods that have not been added to `helper_method` arguments. I prefer defining helper methods as `protected` and then speccing with `expect(subject.send(:your_helper_method)).to eq "a_helpful_string"`. – Epigene Feb 02 '17 at 10:23
  • send is a hack. don't do that – Ryan Buckley Oct 05 '17 at 19:12
  • 1
    A hack can serve the purpose, please refer to the alternative if this is the canonical way of doing it. Plus, send is part of the language specification for a reason. – Konrad Reiche Oct 06 '17 at 21:43