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.
You can override user-agent set in the request env by doing the following.
Then, in your spec:
for those who use request specs instead of controller specs and want to set
request.env
can do it like this:this will make
request.env["whatever"]
available in your controllers with value you gave it in your specs.Well, you've almost nothing to do:
I just gave this another try and this passes:
Try this:
stub(request).env { {"HTTP_USER_AGENT" => "Some String"} }
If you're using rspec-rails, you might be able to use
controller.request
in your helper tests.