Ken Egervari
2011-May-25 20:50 UTC
[rspec-users] How do you factor out common "before(:each)" calls so that multiple specs can use them?
I''d like to factor this bunch of code so that all of my controller tests (well, almost all of them) use this before(:each) block: before(:each) do @user = User.new controller.stub(:authenticate_user!) controller.stub(:current_user).and_return(@user) controller.stub(:add_secure_model_data) end Is there any way to do that? I don''t want to include it in all controllers... because there are a few that don''t need this. By basically, every controller that extends from SecureController will need this before(:each) block. Is there any nice way to do that? Thanks Ken -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20110525/4fe08ae2/attachment.html>
David Chelimsky
2011-May-25 21:26 UTC
[rspec-users] How do you factor out common "before(:each)" calls so that multiple specs can use them?
On May 25, 2011, at 3:50 PM, Ken Egervari wrote:> I''d like to factor this bunch of code so that all of my controller tests (well, almost all of them) use this before(:each) block: > > before(:each) do > @user = User.new > controller.stub(:authenticate_user!) > controller.stub(:current_user).and_return(@user) > controller.stub(:add_secure_model_data) > end > > Is there any way to do that? I don''t want to include it in all controllers... because there are a few that don''t need this. By basically, every controller that extends from SecureController will need this before(:each) block. > > Is there any nice way to do that?http://relishapp.com/rspec/rspec-core/v/2-6/dir/example-groups/shared-context Cheers, David
Sidu Ponnappa
2011-May-25 21:48 UTC
[rspec-users] How do you factor out common "before(:each)" calls so that multiple specs can use them?
Ken, I''ve sometimes found extracting this code into a helper like def login(user) @controller.stub(:authenticate_user!) @controller.stub(:current_user).and_return(user) @controller.stub(:add_secure_model_data) user end and before(:each) @user = login(User.new) end because most of my controller spec files have to have specs covering unautheticated attempts to access a secured action, or the User may have to be constructed in different ways, and this approach makes it easier to have scenarios for both authenticated and unauthenticated access in the same spec. YMMV. Best, Sidu. http://c42.in http://about.me/ponnappa On 26 May 2011 02:20, Ken Egervari <ken.egervari at gmail.com> wrote:> I''d like to factor this bunch of code so that all of my controller tests > (well, almost all of them) use this before(:each) block: > > ? before(:each) do > ??? @user = User.new > ??? controller.stub(:authenticate_user!) > ??? controller.stub(:current_user).and_return(@user) > ??? controller.stub(:add_secure_model_data) > ? end > > Is there any way to do that? I don''t want to include it in all > controllers... because there are a few that don''t need this. By basically, > every controller that extends from SecureController will need this > before(:each) block. > > Is there any nice way to do that? > > Thanks > > Ken > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
Ken Egervari
2011-May-25 22:16 UTC
[rspec-users] How do you factor out common "before(:each)" calls so that multiple specs can use them?
That''s a really good suggestion Sidu. As I''ve been refactoring my tests, I have actually come across such as a case where @admin is the current signed in user, but I am testing the UsersController, so I also have @user ;) Can be confusing... lol. Ken On Wed, May 25, 2011 at 5:48 PM, Sidu Ponnappa <ckponnappa at gmail.com> wrote:> Ken, > > I''ve sometimes found extracting this code into a helper like > > def login(user) > @controller.stub(:authenticate_user!) > @controller.stub(:current_user).and_return(user) > @controller.stub(:add_secure_model_data) > user > end > > and > > before(:each) > @user = login(User.new) > end > > because most of my controller spec files have to have specs covering > unautheticated attempts to access a secured action, or the User may > have to be constructed in different ways, and this approach makes it > easier to have scenarios for both authenticated and unauthenticated > access in the same spec. YMMV. > > Best, > Sidu. > http://c42.in > http://about.me/ponnappa > > On 26 May 2011 02:20, Ken Egervari <ken.egervari at gmail.com> wrote: > > I''d like to factor this bunch of code so that all of my controller tests > > (well, almost all of them) use this before(:each) block: > > > > before(:each) do > > @user = User.new > > controller.stub(:authenticate_user!) > > controller.stub(:current_user).and_return(@user) > > controller.stub(:add_secure_model_data) > > end > > > > Is there any way to do that? I don''t want to include it in all > > controllers... because there are a few that don''t need this. By > basically, > > every controller that extends from SecureController will need this > > before(:each) block. > > > > Is there any nice way to do that? > > > > Thanks > > > > Ken > > > > _______________________________________________ > > rspec-users mailing list > > rspec-users at rubyforge.org > > http://rubyforge.org/mailman/listinfo/rspec-users > > > _______________________________________________ > 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/20110525/7becedeb/attachment-0001.html>