我有几个星期的问题在示例书第7章的学习轨道上,在本章的最后我在rspec规范中得到这些错误消息
1)UsersController应该有正确的标题失败/错误:get:show,:id => @user ActionController :: RoutingError:没有路由匹配{:id => nil,:controller =>“users”,:action =>“show”}#。/ spec / control / users_controller_spec.rb:36:in '阻止(2级)'
2)UsersController应该包含用户名失败/错误:get:show,:id => @user ActionController :: RoutingError:没有路由匹配{:id => nil,:controller =>“users”,:action =>“show”}#。/ spec / control / users_controller_spec.rb:41:in '阻止(2级)'
3)UsersController应该有个人资料图片失败/错误:get:show,:id => @user ActionController :: RoutingError:没有路由匹配{:id => nil,:controller =>“users”,:action =>“show”}#。/ spec / control / users_controller_spec.rb:46:in '阻止(2级)'
我应该提到页面有效,我创建了2个用户
以下是我所做的所有相关代码,
规格/ factories.rb
Factory.define :user do |user|
user.name "Michael Hartl"
user.email "mhartl@example.com"
user.password "foobar"
user.password_confirmation "foobar"
end
应用程序/视图/用户/ show.html.erb
<%= @user.name %>, <%= @user.email %>
<table class="profile" summary="Profile Information">
<tr>
<td class="main">
<h1>
<%= gravatar_for @user %>
<%= @user.name %>
</h1>
</td>
<td class="sidebar round">
<strong>Name</strong> <%= @user.name %><br />
<strong>URL</strong> <%= link_to user_path(@user), @user %>
</td>
</tr>
</table>
应用程序/助手/ users_helper.rb
module UsersHelper
def gravatar_for(user, options = { :size => 50 })
gravatar_image_tag(user.email.downcase, :alt => user.name,
:class => 'gravatar',
:gravatar => options)
end
end
应用程序/控制器/ Users_controller.rb
class UsersController < ApplicationController
def show
@user = User.find(params[:id])
@title = @user.name
end
Users_controller_spec.rb
require 'spec_helper'
describe UsersController do
render_views
describe "GET 'show'" do
before(:each) do
@user = Factory(:user)
# User.stub!(:find, @user.id) .and_return(@user)
end
it "should be successful" do
get :show, :id => @user
response.should be_success
end
it "should find the right user" do
get :show, :id => @user
assigns(:user).should == @user
end
end
describe "GET 'new'" do
it "should be successful" do
get 'new'
response.should be_success
end
it "should have the right title" do
get 'new'
response.should have_selector("title", :content => "Sign up")
end
end
it "should have the right title" do
get :show, :id => @user
response.should have_selector("title", :content => @user.name)
end
it "should include the user's name" do
get :show, :id => @user
response.should have_selectori("h1", :content => @user.name)
end
it "should have a profile image" do
get :show, :id => @user
response.should have_selector("h1>img", :class => "gravatar")
end
end
路由 SampleApp :: Application.routes.draw做 资源:用户
match '/signup', :to => 'users#new'
match '/contact', :to => 'pages#contact'
match '/about', :to => 'pages#about'
match '/help', :to => 'pages#help'
root :to => 'pages#home'