Rails: test a helper that needs access to the Rails environment (e.g. request.fullpath)

心已入冬 提交于 2019-12-22 06:39:31

问题


I have a helper that accesses request.fullpath. Within an isolated helper test, request is not available. What should I do? Can I somehow mock it or something like that?

I'm using the newest versions of Rails and RSpec. Here's what my helper looks like:

def item(*args, &block)
  # some code

  if request.fullpath == 'some-path'
    # do some stuff
  end
end

So the problematic code line is #4 where the helper needs access to the request object which isn't available in the helper spec.

Thanks a lot for help.


回答1:


Yes, you can mock the request. I had a whole long answer here describing how to do that, but in fact that's not necessarily what you want.

Just call your helper method on the helper object in your example. Like so:

describe "#item" do
  it "does whatever" do
    helper.item.should ...
  end
end

That will give you access to a test request object. If you need to specify a specific value for the request path, you can do so like this:

before :each do
  helper.request.path = 'some-path'
end

Actually, for completeness, let me include my original answer, since depending on what you're trying to do it might still be helpful.

Here's how you can mock the request:

request = mock('request')
controller.stub(:request).and_return request

You can add stub methods to the returned request similarly

request.stub(:method).and_return return_value

And alternative syntax to mock & stub all in one line:

request = mock('request', :method => return_value)

Rspec will complain if your mock receives messages that you didn't stub. If there's other stuff Just call your request helper method on the helper object is doing that you don't care about in your test, you can shut rspec up by making the mock a "null object",example. like Like so

 request = mock('request').as_null_object

It looks like all you probably need to get your specific test passing is this:

describe "#item" do
  let(:request){ mock('request', :fullpath => 'some-path') }

  before :each do
    controller.stub(:request).and_return request
  end

  it "does whatever"
end



回答2:


In a helper spec, you can access the request using controller.request (so controller.request.stub(:fullpath) { "whatever" } should work)



来源:https://stackoverflow.com/questions/12528001/rails-test-a-helper-that-needs-access-to-the-rails-environment-e-g-request-fu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!