How to test ApplicationController method defined also as a helper method?

后端 未结 2 1487
你的背包
你的背包 2021-01-31 02:13

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

helper_method :some_method_here

  • How do I test ApplicationController in
相关标签:
2条回答
  • 2021-01-31 02:53

    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.

    0 讨论(0)
  • 2021-01-31 03:00

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

    0 讨论(0)
提交回复
热议问题