简体   繁体   English

Michael Hartl的教程第8章

[英]Michael Hartl's Tutorial Chapter 8

I'm working on MH's tutorial and I keep getting these three errors. 我正在研究MH的教程,并且不断遇到这三个错误。 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 这是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

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. 您已经在signup上下文中定义了let(:submit) { "Create my account" } ,但是after saving the user上下文after saving the user ,您尝试在外部使用它。 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? 老实说,规范的一部分没有意义,您要提交什么?

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. 在保存用户后进行测试您应该在创建用户成功执行此操作,这意味着 with valid information上下文中保存上下文,因此您必须after saving user"sign up"上下文中的"sign up" describe "with valid information" dodescribe "with valid information" doafter saving user嵌套您的上下文,并且您已经具有let(:submit)声明来创建您的Submit变量。

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('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

conclusion : just move after saving the user block before the end of sign up block 结论: after saving the user块之后,才在sign upend之前移动

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM