Alexander Glushkov
2011-Jul-13 15:06 UTC
[rspec-users] rails: action helper for controller specs
Hey guys, just wrote a helper for controller specs in the rails. Link to the gist - https://gist.github.com/1080421 - to view with syntax highlight. # Helper +action+ allows you to write something like # # describe "GET index" do # action { get :index } # # context ''if user signed in'' do # before { sign_in user } # it { should respond_with :success } # end # # context ''if user logged out'' do # it { should redirect_to sign_in_path } # end # end # # instead of # # describe "GET index" do # context ''if user signed in'' do # before { sign_in user } # before { get :index } # it { should respond_with :success } # end # # context ''if user logged out'' do # before { get :index } # it { should redirect_to sign_in_path } # end # end def action(&block) before { self.class.before(&block) unless action_added?(&block) } end def action_added?(&block) self.class.hooks[:before][:each].map(&:to_proc).include? block end What do you think? - Alex -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20110713/40c35d18/attachment.html>
On Wed, Jul 13, 2011 at 11:06 AM, Alexander Glushkov <cutalion at gmail.com>wrote:> Hey guys, > > just wrote a helper for controller specs in the rails. Link to the gist - > https://gist.github.com/1080421 - to view with syntax highlight. > > # Helper +action+ allows you to write something like > # > # describe "GET index" do > # action { get :index } > # > # context ''if user signed in'' do > # before { sign_in user } > # it { should respond_with :success } > # end > # > # context ''if user logged out'' do > # it { should redirect_to sign_in_path } > # end > # end > # > # instead of > # > # describe "GET index" do > # context ''if user signed in'' do > # before { sign_in user } > # before { get :index } > # it { should respond_with :success } > # end > # > # context ''if user logged out'' do > # before { get :index } > # it { should redirect_to sign_in_path } > # end > # end > > > def action(&block) > before { self.class.before(&block) unless action_added?(&block) } > > > > end > > def action_added?(&block) > self.class.hooks[:before][:each].map(&:to_proc).include? block > > > > end > > What do you think? > > > - Alex > > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-usersSo this is just an alias to `before`? -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20110713/aee428e0/attachment.html>
Alexander Glushkov
2011-Jul-13 17:22 UTC
[rspec-users] rails: action helper for controller specs
Not exactly. It adds a before { ''your action call'' } to the end of hooks collection, so that it will called at the very end, right before the spec call. Compare two examples I provided in the comments. -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20110713/0e28c8c9/attachment.html>