Mohamad El-Husseini
2012-Feb-23 23:38 UTC
[rspec-users] Can someone clarify this RSpec block?
I''m new to RSpec, Rails, and Ruby. I''m following the Rails Tutorial. I was going over some RSpec code and go wanted some clarification: describe "User pages" do subject { page } describe "sign up" do describe "with valid information" do before do fill_in "Name", with: "Mickey Mouse" fill_in "Email", with: "mickey at disney.com" fill_in "Password", with: "m1ck3y" fill_in "Confirmation", with: "m1ck3y" end it "should create a user" do # block 1 expect { click_button "Sign up" }.to change(User, :count).by(1) end describe "after saving the user" do #block 2 before { click_button ''Sign up'' } let(:user) { User.find_by_email(''mickey at disney.com'') } it { should have_selector(''title'', text: user.name) } it { should have_link(''Sign out'') } end end Notice the "it ''should create a user''" block. Why doesn''t the user we create in that block carry over to the next block? ("after saving the user")? Why is it necessary to write before { click_button ''Sign up'' } in order to test for the user? Shouldn''t the user already exist as it was created in block 2? I''m looking to understand the flow of events in RSpec. Thanks! -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20120223/32adfa90/attachment.html>
On Feb 23, 2012, at 4:38 PM, Mohamad El-Husseini wrote:> I''m new to RSpec, Rails, and Ruby. I''m following the Rails Tutorial. I was going over some RSpec code and go wanted some clarification: > > describe "User pages" do > subject { page } > > describe "sign up" do > describe "with valid information" do > before do > fill_in "Name", with: "Mickey Mouse" > fill_in "Email", with: "mickey at disney.com" > fill_in "Password", with: "m1ck3y" > fill_in "Confirmation", with: "m1ck3y" > end > > it "should create a user" do # block 1 > expect { click_button "Sign up" }.to change(User, :count).by(1) > end > > describe "after saving the user" do #block 2 > before { click_button ''Sign up'' } > let(:user) { User.find_by_email(''mickey at disney.com'') } > it { should have_selector(''title'', text: user.name) } > it { should have_link(''Sign out'') } > end > end > > Notice the "it ''should create a user''" block. Why doesn''t the user we create in that block carry over to the next block? ("after saving the user")? Why is it necessary to write > > before { click_button ''Sign up'' } in order to test for the user? Shouldn''t the user already exist as it was created in block 2? > > I''m looking to understand the flow of events in RSpec. > > Thanks! > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-usersEach example (`it`) is run in isolation from the other examples -- there is no shared state. This keeps things clean and easy to understand. In your example, just add a method for "signing up": describe "User pages" do subject { page } describe "sign up" do describe "with valid information" do before do fill_in "Name", with: "Mickey Mouse" fill_in "Email", with: "mickey at disney.com" fill_in "Password", with: "m1ck3y" fill_in "Confirmation", with: "m1ck3y" end def click_sign_up_button click_button "Sign up" end it "should create a user" do # block 1 expect { click_sign_up_button }.to change(User, :count).by(1) end describe "after saving the user" do #block 2 before { click_sign_up_button } let(:user) { User.find_by_email(''mickey at disney.com'') } it { should have_selector(''title'', text: user.name) } it { should have_link(''Sign out'') } end end
Matthew Van Horn
2012-Feb-24 15:31 UTC
[rspec-users] Can someone clarify this RSpec block?
Each "it" runs in isolation. The flow is, all the "before" blocks run in order from outer to inner, before each "it" block. You can think of each "it" expectation as sending you to the start, to test another isolated expectation about the process. This is good, because a failure in one part of the system won''t necessarily break tests for another part. -- matt On Feb 23, 2012, at 3:38 PM, Mohamad El-Husseini <husseini.mel at gmail.com> wrote:> I''m new to RSpec, Rails, and Ruby. I''m following the Rails Tutorial. I was going over some RSpec code and go wanted some clarification: > > describe "User pages" do > subject { page } > > describe "sign up" do > describe "with valid information" do > before do > fill_in "Name", with: "Mickey Mouse" > fill_in "Email", with: "mickey at disney.com" > fill_in "Password", with: "m1ck3y" > fill_in "Confirmation", with: "m1ck3y" > end > > it "should create a user" do # block 1 > expect { click_button "Sign up" }.to change(User, :count).by(1) > end > > describe "after saving the user" do #block 2 > before { click_button ''Sign up'' } > let(:user) { User.find_by_email(''mickey at disney.com'') } > it { should have_selector(''title'', text: user.name) } > it { should have_link(''Sign out'') } > end > end > > Notice the "it ''should create a user''" block. Why doesn''t the user we create in that block carry over to the next block? ("after saving the user")? Why is it necessary to write > > before { click_button ''Sign up'' } in order to test for the user? Shouldn''t the user already exist as it was created in block 2? > > I''m looking to understand the flow of events in RSpec. > > Thanks! > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users