[rspec-users] Testing a before_filter with a Rails.env condition
dchelimsky at gmail.com
Mon Aug 29 22:34:31 EDT 2011
On Aug 29, 2011, at 6:09 PM, Shane Mingins wrote:
> We have the occasional ApplicationController before_filter that is conditioned by the Rails.env and we like the following style:
> before_filter :check_for_something if Rails.env == 'production'
This approach won't work because this line is eval'd once and only once when the file is loaded.
> And wish to spec this in an ApplicationController spec using an anonymous controller (e.g. http://relishapp.com/rspec/rspec-rails/docs/controller-specs/anonymous-controller)
> Q. Just wondering how we can Rails.stub(:env).and_return('production') to trigger the before_filter?
> As it stands we have moved the conditional down into the method and stub in the before(:each)
> before_filter :check_for_something
> def check_for_something
> if Rails.env == 'production'
> before(:each) do
This approach works because Rails.env inside the check_for_something method is eval'd each time the method is called.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the rspec-users