0

我花了几个小时阅读这本书和 github 页面上的代码,但我似乎找不到我的答案。我在第 7.1.3 章中,并试图通过以下测试。据我所知,当我查看页面时,一切正常。

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 Up') }               (10)
    it { should have_selector('title',  text: full_title('Sign Up')) }   (11)
  end

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

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

我浏览了所有我能想到的文件,检查可能出了什么问题。

应用程序/视图/用户/new.html.erb

<% provide(:title, 'Sign Up') %>
<h1>Sign Up</h1>
<p>Find me in app/views/users/new.html.erb</p>

**app/views/users/show.html.erb

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

应用程序/控制器/users_controller.rb

class UsersController < ApplicationController

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

  def new
  end

end

配置/路由.rb

SampleApp::Application.routes.draw do

  resources :users

  match '/contact', :to => 'pages#contact'
  match '/about', :to => 'pages#about'
  match '/help', :to => 'pages#help'

  match '/signup', :to => 'users#new'

  root :to => 'pages#home'

我想我已经附上了所有相关的代码,如果添加任何其他内容会有所帮助,请告诉我。以下是我得到的实际错误。如果你能指出我在工作中扔了扳手的地方,我将非常感激。

rspec ./spec/requests/user_pages_spec.rb:10 # User pages signup page 
rspec ./spec/requests/user_pages_spec.rb:11 # User pages signup page 
rspec ./spec/requests/user_pages_spec.rb:18 # User pages profile page 
rspec ./spec/requests/user_pages_spec.rb:19 # User pages profile page 

感谢您的帮助和时间!

4

1 回答 1

0

删除第 18 行和第 19 行中的引号。那应该可以通过您的测试。

像这样:

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

还要查找您的 routes.rb。在rails教程中很好

match '/help',    to: 'static_pages#help'
match '/about',   to: 'static_pages#about'
match '/contact', to: 'static_pages#contact'

除非您使用名称页面创建静态页面视图。然后忘记我的 routes.rb

于 2013-06-19T21:04:00.693 回答