第7章Ruby on Rails users_controller.rb错误

时间:2012-06-03 19:29:13

标签: ruby-on-rails ruby-on-rails-3 rspec

我按照第7章Ruby on Rails教程中的所有说明进行操作,当我最终运行$ Bundle exec rspec spec /时会显示一系列错误。我加倍检查我的代码是否有任何拼写错误或遗漏任何符号。一切似乎都很好。能否请您解释错误并告诉我哪里出错了?谢谢

USER_PAGES_SPEC.RB

require 'spec_helper'

describe "User pages" do

  subject { page }

  describe "signup page" do
  before {visit signup_path}

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

 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" 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

      describe "after saving a user" do
        before {click_button submit}

        let (:user) {user.find_by_email("user@example.com")}

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

USER_CONTROLLER.RB

class UsersController < ApplicationController

  def show
    @user = User.find(params[:id])
  end

  def new
    @user = User.new
  end

  def create
    @user = User.new(params[:user])

    if @user.save

    sign_in @user

    flash[:success] = "Welcome to the Sample App!"

    redirect_to @user

    else

     render 'new'
    end
  end
end

显示在命令行上的错误

    3) User pages signup page
 ←[31mFailure/Error:←[0m ←[31mit { should have_selector('title', text: full_

标题('注册'))}←[0m      ←[31mNoMethodError:←[0米        ←[31]定义方法full_title' for #<RSpec::Core::ExampleGroup::Neste d_2::Nested_1:0x461b260>←[0m ←[36m # ./spec/requests/user_pages_spec.rb:11:in阻止(3级)'←[0m

4)用户页面个人资料页面      ←[31mFailure /错误:←[0m←[31mit {should have_selector('title',text:user。 姓名)}←[0m        ←[31mexpected css“title”与文本“Michael Hartl”返回一些东西←[ 0米 ←[36m#./spec/requests/user_pages_spec.rb:19:in`clock(3级)in←[0m

5)用有效信息注册的用户页面应该创建一个用户      ←[31mFailure /错误:←[0m←[31mexpect {click_button submit} .to change(User,  :计数)。通过(1)←[0米      ←[31mNoMethodError:←[0米        ←[31mundefined method sign_in' for #<UsersController:0x45a8f00>←[0m ←[36m # ./app/controllers/users_controller.rb:14:in create'←[0m ←[36m#(eval):2:click_button'←[0m ←[36m # ./spec/requests/user_pages_spec.rb:43:in块(5级)in←[0m ←[36m#./spec/requests/user_pages_spec.rb:43:in`block(4级)in←[0m

6)用户页面在保存用户后注册有效信息      ←[31mFailure /错误:←[0m←[31mbefore {click_button submit}←[0m      ←[31mNoMethodError:←[0米        ←[31mundefined method sign_in' for #<UsersController:0x46341c0>←[0m ←[36m # ./app/controllers/users_controller.rb:14:in create'←[0m ←[36m#(eval):2:click_button'←[0m ←[36m # ./spec/requests/user_pages_spec.rb:47:in区块(5级)'←[0m

1 个答案:

答案 0 :(得分:0)

  

1)未定义的方法full_title

你定义了吗?

  

2)预期css“title”与文本“Michael Hartl”返回一些东西

输出html中没有<title>Michael Hartl</title>

  

3,4)未定义的方法sign_in

你在哪里定义它?

更正您的代码,所有测试都将通过。