开发者

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

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

helper_method :some_meth开发者_StackOverflow中文版od_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


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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜