开发者

Use request helpers outside of controller specs

I'd like to incl开发者_开发知识库ude the request helpers (from ActionDispatch::Integration::RequestHelpers [ApiDock], like post and xhr methods) also in some specs outside of my controller specs. The problem is that these request helpers are only included in spec/controller and when a controller is described. What do I have to include/require in those specs? I am using RSpec 2 and Rails 3.


I just solved the problem by including the below code in my acceptance helper. If you are not Steak then just simply put it in spec helper or require it from somewhere else. post and xhr methods are now available in that spec regardless in what spec it is or in what directory you are.

The code is derived from RSpec::Rails::RequestExampleGroup

RSpec::Core::ExampleGroup.class_eval do
  include ActiveSupport::Concern
  include ActionDispatch::Integration::Runner
  include RSpec::Rails::BrowserSimulators

  def app
    ::Rails.application
  end

  def last_response
    response
  end
end


I know it's 4 years later and many things have of course changed, but since I stumbled on this question while searching how to make other tests behave like controller tests (and thus have post and get methods and the like) I wanted to point out this solution that works with RSpec 3: if you add this to the spec_helper

 config.include RSpec::Rails::RequestExampleGroup, type: :request, example_group: { file_path: /spec\/(api|integration)/

it will make all tests in the given path have support for controller methods.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜