[rspec-users] Can someone clarify this RSpec block?

Mohamad El-Husseini husseini.mel at gmail.com
Thu Feb 23 23:38:39 UTC 2012

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"

    it "should create a user" do # block 1
     expect { click_button "Sign up" }.to change(User, :count).by(1)

    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') }

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.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20120223/32adfa90/attachment.html>

More information about the rspec-users mailing list