失败:用户页面配置文件页面应具有标题“示例用户”

时间:2013-08-05 16:01:42

标签: rspec devise ruby-on-rails-4 railstutorial.org

我正在关注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的新手,这是我的第一个教程。我不能为我的生活找出错误,即使解决方案似乎很简单?

1 个答案:

答案 0 :(得分: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