Ruby on Rails故障/错误教程第9章

时间:2013-12-14 02:31:54

标签: ruby-on-rails rspec

任何做过这个练习的人都可以帮助我吗?

这是我第一次使用这种编程语言而对此并不多,但我需要在第10章完成之前完成最终评估。

1) User pages edit with valid information 
     Failure/Error: describe "page" do
     NoMethodError:
       undefined method `describe' for    #<RSpec::Core::ExampleGroup::Nested_5::Nested_5::Nested_1:0xc283f8c>
     # ./spec/requests/user_pages_spec.rb:87:in `block (3 levels) in <top (required)>'

  2) User pages edit with valid information 
     Failure/Error: describe "page" do
     NoMethodError:
       undefined method `describe' for #<RSpec::Core::ExampleGroup::Nested_5::Nested_5::Nested_1:0xb67b1cc>
     # ./spec/requests/user_pages_spec.rb:87:in `block (3 levels) in <top (required)>'

  3) User pages edit with valid information 
     Failure/Error: describe "page" do
     NoMethodError:
       undefined method `describe' for #<RSpec::Core::ExampleGroup::Nested_5::Nested_5::Nested_1:0xc224244>
     # ./spec/requests/user_pages_spec.rb:87:in `block (3 levels) in <top (required)>'

  4) User pages edit with valid information 
     Failure/Error: describe "page" do
     NoMethodError:
       undefined method `describe' for #<RSpec::Core::ExampleGroup::Nested_5::Nested_5::Nested_1:0xc562690>
     # ./spec/requests/user_pages_spec.rb:87:in `block (3 levels) in <top (required)>'

  5) User pages edit with valid information 
     Failure/Error: describe "page" do
     NoMethodError:
       undefined method `describe' for #<RSpec::Core::ExampleGroup::Nested_5::Nested_5::Nested_1:0xc0559a4>
     # ./spec/requests/user_pages_spec.rb:87:in `block (3 levels) in <top (required)>'

  6) User pages index 
     Failure/Error: visit users_path
     ActionView::Template::Error:
       undefined method `each' for nil:NilClass
     # ./app/views/users/index.html.erb:5:in `_app_views_users_index_html_erb__615083638_97671840'
     # ./spec/requests/user_pages_spec.rb:12:in `block (3 levels) in <top (required)>'

  7) User pages index 
     Failure/Error: visit users_path
     ActionView::Template::Error:
       undefined method `each' for nil:NilClass
     # ./app/views/users/index.html.erb:5:in `_app_views_users_index_html_erb__615083638_97671840'
     # ./spec/requests/user_pages_spec.rb:12:in `block (3 levels) in <top (required)>'

  8) User pages index should list each user
     Failure/Error: visit users_path
     ActionView::Template::Error:
       undefined method `each' for nil:NilClass
     # ./app/views/users/index.html.erb:5:in `_app_views_users_index_html_erb__615083638_97671840'
     # ./spec/requests/user_pages_spec.rb:12:in `block (3 levels) in <top (required)>'

  9) Authentication authorization after signing in should render the desired protected page
     Failure/Error: expect(page).to have_title('Edit user')
       expected #has_title?("Edit user") to return true, got false
     # ./spec/requests/authentication_pages_spec.rb:84:in `block (4 levels) in <top (required)>'

Finished in 2.25 seconds
72 examples, 9 failures

Failed examples:

rspec ./spec/requests/user_pages_spec.rb:114 # User pages edit with valid information 
rspec ./spec/requests/user_pages_spec.rb:115 # User pages edit with valid information 
rspec ./spec/requests/user_pages_spec.rb:112 # User pages edit with valid information 
rspec ./spec/requests/user_pages_spec.rb:113 # User pages edit with valid information 
rspec ./spec/requests/user_pages_spec.rb:111 # User pages edit with valid information 
rspec ./spec/requests/user_pages_spec.rb:15 # User pages index 
rspec ./spec/requests/user_pages_spec.rb:16 # User pages index 
rspec ./spec/requests/user_pages_spec.rb:18 # User pages index should list each user
rspec ./spec/requests/authentication_pages_spec.rb:83 # Authentication authorization after signing in should render the desired protected page

修改

这是我的代码:

require 'spec_helper'

describe "User pages" do

  subject { page }

  describe "index" do
    before do
      sign_in FactoryGirl.create(:user)
      FactoryGirl.create(:user, name: "Bob", email: "bob@example.com")
      FactoryGirl.create(:user, name: "Ben", email: "ben@example.com")
      visit users_path
    end

    it { should have_title('All users') }
    it { should have_content('All users') }

    it "should list each user" do
      User.all.each do |user|
        expect(page).to have_selector('li', text: user.name)
      end
    end
  end

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

    it { should have_content(user.name) }
    it { should have_title(user.name) }
  end

  describe "signup page" do
    before { visit signup_path }

    it { should have_content('Sign up') }
    it { should have_title(full_title('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
      describe "after submission" do
        before { click_button submit }

        it { should have_title('Sign up') }
        it { should have_content('error') }
      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

      describe "after saving the user" do
        before { click_button submit }
        let(:user) { User.find_by(email: 'user@example.com') }

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

    describe "edit" do
       let(:user) { FactoryGirl.create(:user) }
    before do
      sign_in user
      visit edit_user_path(user)

    describe "page" do
      it { should have_content("Update your profile") }
      it { should have_title("Edit user") }
      it { should have_link('change', href: 'http://gravatar.com/emails') }
    end

    describe "with invalid information" do
      before { click_button "Save changes" }

      it { should have_content('error') }
    end
  end

   describe "with valid information" do
      let(:new_name)  { "New Name" }
      let(:new_email) { "new@example.com" }
      before do
        fill_in "Name",             with: new_name
        fill_in "Email",            with: new_email
        fill_in "Password",         with: user.password
        fill_in "Confirm Password", with: user.password
        click_button "Save changes"
      end

      it { should have_title(new_name) }
      it { should have_selector('div.alert.alert-success') }
      it { should have_link('Sign out', href: signout_path) }
      specify { expect(user.reload.name).to  eq new_name }
      specify { expect(user.reload.email).to eq new_email }
    end
  end
end

2 个答案:

答案 0 :(得分:1)

如果您的代码格式如下:

describe "edit" do
   let(:user) { FactoryGirl.create(:user) }
before do
  sign_in user
  visit edit_user_path(user)

describe "page" do
  it { should have_content("Update your profile") }
  it { should have_title("Edit user") }
  it { should have_link('change', href: 'http://gravatar.com/emails') }
end

我可以看到,如果没有阅读任何代码,它可能会被破坏。

您的所有代码块是否都已正确关闭?看起来不是这样!

答案 1 :(得分:0)

除了phoet指出的未关闭的块,这些块导致你的“未定义方法描述”错误,你在/app/views/users/index.html.erb第5行出现错误。错误是undefined method `each' for nil:NilClass,这意味着有你正在使用variable.each进行该行的变量,但是你要么没有设置该变量,要么当你设置它时,它被设置为nil

通常,您成功完成此操作的方法是阅读错误消息并尝试修复他们抱怨的内容。如果您单独搜索每个错误消息,您将找到有关特定问题的堆栈溢出问题,这些问题将对您有所帮助。