0
votes

I'm working on MH's tutorial and I keep getting these three errors. It's saying that there 'submit' is an undefined variable and I don't see why. Not sure what the problem is. Any suggestions

Failures:

  1) User pages after saving the user 
     Failure/Error: before { click_button submit }
     NameError:
       undefined local variable or method `submit' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_4:0x007f8e816f7c70>
     # ./spec/requests/user_pages_spec.rb:50:in `block (3 levels) in <top (required)>'

  2) User pages after saving the user 
     Failure/Error: before { click_button submit }
     NameError:
       undefined local variable or method `submit' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_4:0x007f8e841d4740>
     # ./spec/requests/user_pages_spec.rb:50:in `block (3 levels) in <top (required)>'

  3) User pages after saving the user 
     Failure/Error: before { click_button submit }
     NameError:
       undefined local variable or method `submit' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_4:0x007f8e84265d80>
     # ./spec/requests/user_pages_spec.rb:50:in `block (3 levels) in <top (required)>'

Finished in 0.00577 seconds
3 examples, 3 failures

Failed examples:

rspec ./spec/requests/user_pages_spec.rb:53 # User pages after saving the user 
rspec ./spec/requests/user_pages_spec.rb:55 # User pages after saving the user 
rspec ./spec/requests/user_pages_spec.rb:54 # User pages after saving the user 

This is the user_pages_spec file

require 'spec_helper'

describe "User pages" do

    subject { page }

    describe "profile page" do
        let(:user) { FactoryGirl.create(:user) }

        before { visit user_path(user) }

        it { should have_selector('h1',    text: user.name) }
        it { should have_selector('title', text: user.name) }
    end

    describe "signup page" do
        before { visit signup_path }

        it { should have_selector('h1',    text: 'Sign up') }
        it { should have_selector('title', text: 'Sign up') }
    end

    describe "signup" do

        before { visit signup_path }

        let(:submit) { "Create my account" }

        describe "with invalid information" do
          it "should not create a user" do
            expect { click_button submit }.not_to change(User, :count)
          end
        end

        describe "with valid information" do
            before do
                fill_in "Name",          with: "Example User"
                fill_in "Email",         with: "[email protected]"
                fill_in "Password",      with: "foobar"
                fill_in "Confirmation",  with: "foobar"
            end

            it "should create a user" do
                expect { click_button submit }.to change(User, :count).by(1)
            end
        end
    end

    describe "after saving the user" do
        before { click_button submit }
        let(:user) { User.find_by_email('[email protected]') }

        it { should have_selector('title', text: user.name) }
        it { should have_selector('div.alert.alert-success', text: 'Welcome') }
        it { should have_link('Sign out') }
    end
end
2

2 Answers

1
votes

I fixed your formatting quick in the spec, take a look now and it should be more obvious why...

You have let(:submit) { "Create my account" } defined in the signup context, but you're trying to use it outside, in the after saving the user context. You need to move it out so both can see it, or re-define it within the other context.

To be honest that part of your spec kind of doesn't make sense, what are you submitting?

0
votes

To test after saving user block you should do this when creating user succeed this means inside with valid information context, so you must just nest your context after saving user inside the describe "with valid information" do which is in the "sign up" context, and you have already let(:submit) declaration which create your submit variable.

Your code will looks like this :

describe "sign up" do 

  before { visit signup_path } 

  let(:submit) { "Create my account" } 
  .
  .
  .
  .

  describe "with valid information" do

    # here you fill fields for signing up a new user
    .
    .
    .
    describe "after saving the user" do
      # when you are here, you have already filling fields of signup form, so you should submit these fields and the code below let you do this
      before { click_button submit }
      let(:user) { User.find_by_email('[email protected]') }

      it { should have_selector('title', text: user.name) }
      it { should have_selector('div.alert.alert-success', text: 'Welcome') }
      it { should have_link('Sign out') }
    end
  end
end

conclusion : just move after saving the user block before the end of sign up block