Michael Hartl的教程第8章

时间:2013-08-27 20:51:36

标签: ruby-on-rails ruby

我正在研究MH的教程,我不断收到这三个错误。它说'提交'是一个未定义的变量,我不明白为什么。不确定是什么问题。任何建议

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 

这是user_pages_spec文件

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: "user@example.com"
                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('user@example.com') }

        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 个答案:

答案 0 :(得分:1)

我在规范中快速修改了你的格式,现在看看,为什么......

应该更明显

您在let(:submit) { "Create my account" }上下文中定义了signup,但您正试图在after saving the user上下文中使用它。您需要将其移出,以便两者都可以看到它,或者在其他上下文中重新定义它。

说实话,你的那部分规格没有意义,你提交的是什么?

答案 1 :(得分:0)

要在保存用户块后测试,您应该在创建用户成功时执行此操作,这意味着 with valid information上下文中,因此您必须嵌套您的上下文{{ 1}}位于after saving user上下文中的describe "with valid information" do内,您已经 let(:submit)声明创建了您的提交变量。

您的代码如下所示:

"sign up"

结论:只需在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('user@example.com') } 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 阻止after saving the user之前移动end阻止