0

我正在关注 Michael Hartl 的 rails 教程,我在第 7 章的中间,尝试配置用户配置文件。我正在使用设计,省略了创建views/users/new.html.erb页面,而是将其保留为设计的默认值app/views/devise/registrations/new.html.erb。但是,对于配置文件页面,我创建了一个views/users/show.html.erb文件并从我的文件上的设计默认值重新路由,config/routes.rb如下所示:get 'show/:id' => 'user#show'.

我对我的user_pages_spec.rb文件实施了适当的规范测试:

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_content(user.name) }
        it { should have_title(user.name) }
    end


end

我的users_controller.rb文件:

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

    def new
    end

结尾

还有我的show.html.erb文件:

<% provide(:title, @user.name) %>
<h1><%= @user.name %></h1>

当我运行测试时,我收到此错误:

Failures:

  1) User pages profile page should have title "Example User"
     Failure/Error: it { should have_title(user.name) }
     expected #has_title?("Example User") to return true, got false
     # ./spec/requests/user_pages_spec.rb:12:in `block (3 levels) in <top (required)>'

Finished in 2.69 seconds
8 examples, 1 failure

Failed examples:

rspec ./spec/requests/user_pages_spec.rb:12 # User pages profile page should have title              "Example User"

我是 Rails 的新手,这是我的第一个教程。即使解决方案似乎很简单,我也无法终生找出错误?

4

1 回答 1

0

尝试将此代码添加到您的 user_pages_spec.rb 文件

subject { page }

describe "profile page" do
    let(:user) { FactoryGirl.create(:user) }
    before do
      sign_in user <---------------
      visit user_path(user)
    end  
    it { should have_content(user.name) }
    it { should have_title(user.name) }
end
于 2013-10-12T19:28:13.180 回答