Rspec: how to spec request.env in a helper spec?

RoundOutTooSoon picture RoundOutTooSoon · Apr 12, 2012 · Viewed 13.4k times · Source

In my helper module, I have:

def abc(url)
  ...
  if request.env['HTTP_USER_AGENT']
    do something
  end
end

In my spec file, I have:

  describe "#abc" do      
before(:each) do
  @meth = :abc

  helper.request.env['HTTP_USER_AGENT'] = "..."
end
it "should return the webstart jnlp file" do
  @obj.send(@meth, "some_url").should ....
end
end

When I run the spec I have this error:

undefined local variable or method `request' for <ObjectWithDocHelperMixedIn:0x00000103b5a7d0>

How do I stub for request.env['...'] in my specs?

Thanks.

Answer

Chris Keele picture Chris Keele · Mar 31, 2013

If you're using rspec-rails, you might be able to use controller.request in your helper tests.