0

我正在研究 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
4

2 回答 2

1

我在规范中快速修复了您的格式,现在看一下,应该更清楚为什么......

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

老实说,您的规范的那部分没有意义,您要提交什么?

于 2013-08-27T21:02:23.867 回答
0

在保存用户块后进行测试,您应该在创建用户成功时执行此操作,这意味着 with valid information上下文中,因此您必须将上下文嵌套after saving userdescribe "with valid information" do上下文"sign up"中,并且您已经创建了创建提交变量的let(:submit)声明。

您的代码将如下所示:

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

结论:只需在of块after saving the user之前移动块endsign up

于 2013-08-27T21:13:43.350 回答