Shane Mingins
2011-Aug-29 23:09 UTC
[rspec-users] Testing a before_filter with a Rails.env condition
Hi 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'' 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) e.g. before_filter :check_for_something def check_for_something if Rails.env == ''production'' ... end end before(:each) do Rails.stub(:env).and_return(''production'') end Cheers Shane -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20110830/5abea807/attachment.html>
David Chelimsky
2011-Aug-30 02:34 UTC
[rspec-users] Testing a before_filter with a Rails.env condition
On Aug 29, 2011, at 6:09 PM, Shane Mingins wrote:> Hi > > 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) > > e.g. > > before_filter :check_for_something > > def check_for_something > if Rails.env == ''production'' > ... > end > end > > before(:each) do > Rails.stub(:env).and_return(''production'') > endThis approach works because Rails.env inside the check_for_something method is eval''d each time the method is called. HTH, David -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20110829/6b9572bd/attachment.html>
Shane Mingins
2011-Aug-30 03:27 UTC
[rspec-users] Testing a before_filter with a Rails.env condition
Yeah sorry I realised that was what was happening. But thought I''d ask the question in case there might have been something I was missing (in the docs) or a smart hack ;-) On 30 August 2011 14:34, David Chelimsky <dchelimsky at gmail.com> wrote:> On Aug 29, 2011, at 6:09 PM, Shane Mingins wrote: > > Hi > > 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) > > e.g. > > before_filter :check_for_something > > def check_for_something > if Rails.env == ''production'' > ... > end > end > > before(:each) do > Rails.stub(:env).and_return(''production'') > end > > > This approach works because Rails.env inside the check_for_something method > is eval''d each time the method is called. > > HTH, > David > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20110830/1b4e5254/attachment-0001.html>