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.
Request specs give you the ability to test what your application does, rather than how it does it. For example, instead of testing that the right template is rendered in a controller spec, with a request spec we can test that the content we are expecting to appear actually appears in the response body.
Request specs allow you to focus on a single controller action, but unlike controller tests involve the router, the middleware stack, and both rack requests and responses. This adds realism to the test that you are writing, and helps avoid many of the issues that are common in controller specs.
request. env is a ruby hash that contains information about a visiting user's and server environments. request. env is the standard object that's being used in Rails app to extract important information such as path_info , request_uri etc.
A controller spec is an RSpec wrapper for a Rails functional test. (ActionController::TestCase::Behavior). It allows you to simulate a single http request in each example, and then. specify expected outcomes such as: rendered templates.
If you're using rspec-rails, you might be able to use controller.request
in your helper tests.
Well, you've almost nothing to do:
before(:each) do @meth = :abc request.env['HTTP_USER_AGENT'] = "..." end
I just gave this another try and this passes:
#in helper def foo request.env['HTTP_USER_AGENT'] end #spec it "foo" do helper.request.env['HTTP_USER_AGENT'] = 'foo' expect(helper.foo).to eq 'foo' end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With