In my authenticate_pages.spec (requests) I do the following to test if the signin worked: describe "with valid information" do #let(:account) { FactoryGirl.create(:account) } let(:user) { FactoryGirl.create(:user) } before do fill_in "Email", with: user.email fill_in "Password", with: user.password click_button "Sign in" end it { should have_link(''Sign out'', href: signout_path) } it { should_not have_link(''Sign in'', href: signin_path) } end Now in my other controllers that assume the user is signed in, how can I refactor this and put it somewhere that I can just call to make the user signed in so I can test pages that assume the user is already signed in? -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20120308/273b16f7/attachment.html>
David Chelimsky
2012-Mar-09 03:38 UTC
[rspec-users] how to refactor signin process for re-use?
On Thu, Mar 8, 2012 at 5:15 PM, S Ahmed <sahmed1020 at gmail.com> wrote:> In my authenticate_pages.spec (requests) I do the following to test if the > signin worked: > > ?describe "with valid information" do > ? ? ? #let(:account) { FactoryGirl.create(:account) } > ? ? ? let(:user) { FactoryGirl.create(:user) } > > ? ? ? before do > ? ? ? ? fill_in "Email", with: user.email > ? ? ? ? fill_in "Password", with: user.password > ? ? ? ? click_button "Sign in" > ? ? ? end > > ? ? ? it { should have_link(''Sign out'', href: signout_path) } > ? ? ? it { should_not have_link(''Sign in'', href: signin_path) } > ? ? end > > > Now in my other controllers that assume the user is signed in, how can I > refactor this and put it somewhere that I can just call to make the user > signed in so I can test pages that assume the user is already signed in?Here''s one pattern I''ve seen (and used): def login_as(user) fill_in "Email", with: user.email fill_in "Password", with: user.password click_button "Sign in" end describe "things" do before { sign_in_as(FactoryGirl.create:(user) } describe "GET /thing" do # ... end end HTH, David
Zach Dennis
2012-Mar-09 14:27 UTC
[rspec-users] how to refactor signin process for re-use?
On Thu, Mar 8, 2012 at 10:38 PM, David Chelimsky <dchelimsky at gmail.com> wrote:> On Thu, Mar 8, 2012 at 5:15 PM, S Ahmed <sahmed1020 at gmail.com> wrote: >> In my authenticate_pages.spec (requests) I do the following to test if the >> signin worked: >> >> ?describe "with valid information" do >> ? ? ? #let(:account) { FactoryGirl.create(:account) } >> ? ? ? let(:user) { FactoryGirl.create(:user) } >> >> ? ? ? before do >> ? ? ? ? fill_in "Email", with: user.email >> ? ? ? ? fill_in "Password", with: user.password >> ? ? ? ? click_button "Sign in" >> ? ? ? end >> >> ? ? ? it { should have_link(''Sign out'', href: signout_path) } >> ? ? ? it { should_not have_link(''Sign in'', href: signin_path) } >> ? ? end >> >> >> Now in my other controllers that assume the user is signed in, how can I >> refactor this and put it somewhere that I can just call to make the user >> signed in so I can test pages that assume the user is already signed in? > > Here''s one pattern I''ve seen (and used): > > def login_as(user) > ?fill_in "Email", with: user.email > ?fill_in "Password", with: user.password > ?click_button "Sign in" > end > > describe "things" do > ?before { sign_in_as(FactoryGirl.create:(user) } > ?describe "GET /thing" do > ? ?# ... > ?end > endWhere #sign_in_as and #login_as in David''s example should be the same method. Zach> > HTH, > David > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users-- -- @zachdennis http://www.continuousthinking.com http://www.mutuallyhuman.com
Sam Goldman
2012-Mar-09 15:24 UTC
[rspec-users] how to refactor signin process for re-use?
We have used shared contexts to achieve this. https://www.relishapp.com/rspec/rspec-core/docs/example-groups/shared-context For controller specs using devise, assuming we have let(:current_user) in the test including this shared context: shared_context :signed_in do before do sign_in(current_user) controller.stub!(:current_user).and_return(current_user) end end On Thu, Mar 8, 2012 at 6:15 PM, S Ahmed <sahmed1020 at gmail.com> wrote:> In my authenticate_pages.spec (requests) I do the following to test if the > signin worked: > > ?describe "with valid information" do > ? ? ? #let(:account) { FactoryGirl.create(:account) } > ? ? ? let(:user) { FactoryGirl.create(:user) } > > ? ? ? before do > ? ? ? ? fill_in "Email", with: user.email > ? ? ? ? fill_in "Password", with: user.password > ? ? ? ? click_button "Sign in" > ? ? ? end > > ? ? ? it { should have_link(''Sign out'', href: signout_path) } > ? ? ? it { should_not have_link(''Sign in'', href: signin_path) } > ? ? end > > > Now in my other controllers that assume the user is signed in, how can I > refactor this and put it somewhere that I can just call to make the user > signed in so I can test pages that assume the user is already signed in? > > > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users
Could I somehow add the method to rspec so its available everywhere? What would the class be so inside the describe block my method will be available w/o having to include it? I''ve seen some people open up the class and add methods to it .... class Test::Unit::TestCase On Fri, Mar 9, 2012 at 10:24 AM, Sam Goldman <samwgoldman at gmail.com> wrote:> We have used shared contexts to achieve this. > > https://www.relishapp.com/rspec/rspec-core/docs/example-groups/shared-context > > For controller specs using devise, assuming we have let(:current_user) > in the test including this shared context: > > shared_context :signed_in do > before do > sign_in(current_user) > controller.stub!(:current_user).and_return(current_user) > end > end > > On Thu, Mar 8, 2012 at 6:15 PM, S Ahmed <sahmed1020 at gmail.com> wrote: > > In my authenticate_pages.spec (requests) I do the following to test if > the > > signin worked: > > > > describe "with valid information" do > > #let(:account) { FactoryGirl.create(:account) } > > let(:user) { FactoryGirl.create(:user) } > > > > before do > > fill_in "Email", with: user.email > > fill_in "Password", with: user.password > > click_button "Sign in" > > end > > > > it { should have_link(''Sign out'', href: signout_path) } > > it { should_not have_link(''Sign in'', href: signin_path) } > > end > > > > > > Now in my other controllers that assume the user is signed in, how can I > > refactor this and put it somewhere that I can just call to make the user > > signed in so I can test pages that assume the user is already signed in? > > > > > > > > _______________________________________________ > > 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/20120309/8159a1e4/attachment.html>