0

我按照第 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_

title('Sign up')) }←[0m ←[31mNoMethodError:←[0m ←[31mundefined方法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/Error:←[0m ←[31mit { should have_selector('title', text: user.name) }←[0m ←[31mexpected css "title" with text "Michael Hartl" to return something←[ 0m ←[36m # ./spec/requests/user_pages_spec.rb:19:in `block (3 levels) in '←[0m

5) 使用有效信息注册的用户页面应该创建一个用户 ←[31mFailure/Error:←[0m ←[31mexpect { click_button submit }.to change(User, :count).by(1)←[0m ←[31mNoMethodError:← [0m ←[31mundefined method sign_in' for #<UsersController:0x45a8f00>←[0m ←[36m # ./app/controllers/users_controller.rb:14:increate'←[0m ←[36m # (eval):2:in click_button'←[0m ←[36m # ./spec/requests/user_pages_spec.rb:43:inblock (5 levels) in '←[0m ←[36m # ./spec/requests/user_pages_spec.rb:43:in ` '←[0m中的块(4级)

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

4

1 回答 1

0

1) 未定义的方法 full_title

你定义了吗?

2) 期望带有文本“Michael Hartl”的css“title”返回一些东西

你还没有<title>Michael Hartl</title>输出html。

3, 4) 未定义的方法sign_in

你在哪里定义的?

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

于 2012-06-04T07:07:37.067 回答