私は今日これに取り組んでいましたが、すべてうまくいっているように見えましたが、rspec を実行せずにかなりの時間が経過したため、多くの障害が発生しています。
RSpec の結果 (本名とメールアドレスは消しました):
Failures:
*deleted, see update below*
7) UsersController GET 'index' for signed-in users should paginate users
Failure/Error: response.should have_selector("a", :href => "/users?escape=false&poge=2", :content => "2")
expected following output to contain a <a href='/users?escape=false&poge=2'>2</a> tag:
<!DOCTYPE html>
<html>
<head>
<title>Ruby on Rails Tutorial Sample App | All users</title>
<!--[if lt IE 9]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]--><link href="/stylesheets/blueprint/screen.css" media="screen" rel="stylesheet" type="text/css">
<link href="/stylesheets/blueprint/print.css" media="print" rel="stylesheet" type="text/css">
<!--[if lt IE 8]><link href="/stylesheets/blueprint/ie.css" media="screen" rel="stylesheet" type="text/css" /><![endif]--><link href="/stylesheets/custom.css?1334507400" media="screen" rel="stylesheet" type="text/css">
</head>
<body>
<div class="container">
<header><a href="/"><img alt="Sample App" class="round" src="/images/logo.png?1333923366"></a>
<nav class="round"><ul>
<li><a href="/">Home</a></li>
<li><a href="/users">Users</a></li>
<li><a href="/users/1">Profile</a></li>
<li><a href="/users/1/edit">Settings</a></li>
<li><a href="/help">Help</a></li>
<li><a href="/signout" data-method="delete" rel="nofollow">Sign out</a></li>
</ul></nav></header><section class="round"><h1>All users</h1>
<div class="pagination">
<span class="previous_page disabled">← Previous</span> <em>1</em> <a rel="next" href="/users?escape=false&page=2">2</a> <a class="next_page" rel="next" href="/users?escape=false&page=2">Next →</a>
</div>
<ul class="users">
<li>
<img alt="myname" class="gravatar" src="http://gravatar.com/avatar/85fb99e31535fbc371865ca9468eae1c?size=30"><a href="/users/1">myname</a>
</li>
<li>
<img alt="myname" class="gravatar" src="http://gravatar.com/avatar/86a7c65390f83bdee8c4455af823b7cb?size=30"><a href="/users/2">myname</a>
</li>
<li>
<img alt="myname" class="gravatar" src="http://gravatar.com/avatar/dd5bff939e78bbc5051efa1a3ff1dcb0?size=30"><a href="/users/3">myname</a>
</li>
<li>
<img alt="myname" class="gravatar" src="http://gravatar.com/avatar/e44c38aa36bffeebb9f59e50de846fb4?size=30"><a href="/users/4">myname</a>
</li>
<li>
<img alt="myname" class="gravatar" src="http://gravatar.com/avatar/125d2f777f0f266ff3fc5e9f79b9b8b7?size=30"><a href="/users/5">myname</a>
</li>
<li>
<img alt="myname" class="gravatar" src="http://gravatar.com/avatar/95a3c1df8f70d065d5f6fe9c799840a9?size=30"><a href="/users/6">myname</a>
</li>
<li>
<img alt="myname" class="gravatar" src="http://gravatar.com/avatar/f41b2a4e646e19853a4396c2d166a078?size=30"><a href="/users/7">myname</a>
</li>
<li>
<img alt="myname" class="gravatar" src="http://gravatar.com/avatar/d31f91433afe441f0cf6ebc9a22aef8d?size=30"><a href="/users/8">myname</a>
</li>
<li>
<img alt="myname" class="gravatar" src="http://gravatar.com/avatar/bb03861fb1123b3afd7e1bb29e9d0b05?size=30"><a href="/users/9">myname</a>
</li>
<li>
<img alt="myname" class="gravatar" src="http://gravatar.com/avatar/c8267a1927445fe261d40a47f53b2ea6?size=30"><a href="/users/10">myname</a>
</li>
<li>
<img alt="myname" class="gravatar" src="http://gravatar.com/avatar/2348e595e0b2331a9d06ed74b309f152?size=30"><a href="/users/11">myname</a>
</li>
<li>
<img alt="myname" class="gravatar" src="http://gravatar.com/avatar/860b2e5e342f55faf0b414f36ab0c948?size=30"><a href="/users/12">myname</a>
</li>
<li>
<img alt="myname" class="gravatar" src="http://gravatar.com/avatar/f4fc0e00abd378811cfa8140be1e1158?size=30"><a href="/users/13">myname</a>
</li>
<li>
<img alt="myname" class="gravatar" src="http://gravatar.com/avatar/fbaf3882d8b938d6e492b7c62a18d3d0?size=30"><a href="/users/14">myname</a>
</li>
<li>
<img alt="myname" class="gravatar" src="http://gravatar.com/avatar/e8c8e9d0376915d55a57fac08ba6e326?size=30"><a href="/users/15">myname</a>
</li>
<li>
<img alt="myname" class="gravatar" src="http://gravatar.com/avatar/65bbf2cc13ab660f9cc879a1308c8a5b?size=30"><a href="/users/16">myname</a>
</li>
<li>
<img alt="myname" class="gravatar" src="http://gravatar.com/avatar/dc892e842ff1fcc90f2e12e111905c70?size=30"><a href="/users/17">myname</a>
</li>
<li>
<img alt="myname" class="gravatar" src="http://gravatar.com/avatar/350c624f4c471c0ea94fe24b3d02fded?size=30"><a href="/users/18">myname</a>
</li>
<li>
<img alt="myname" class="gravatar" src="http://gravatar.com/avatar/4fac89057e3505e044fb8447ab49b0f0?size=30"><a href="/users/19">myname</a>
</li>
<li>
<img alt="myname" class="gravatar" src="http://gravatar.com/avatar/4847c604feadda0992a7be0ed8e000c1?size=30"><a href="/users/20">myname</a>
</li>
<li>
<img alt="myname" class="gravatar" src="http://gravatar.com/avatar/6063db6f5c1108362c76c5a8cd26f046?size=30"><a href="/users/21">myname</a>
</li>
<li>
<img alt="myname" class="gravatar" src="http://gravatar.com/avatar/e4ea822898dad699a636c7efa0418be8?size=30"><a href="/users/22">myname</a>
</li>
<li>
<img alt="myname" class="gravatar" src="http://gravatar.com/avatar/b0be0aebe8cd5972efda62e20e3921ea?size=30"><a href="/users/23">myname</a>
</li>
<li>
<img alt="myname" class="gravatar" src="http://gravatar.com/avatar/3c51b46ac624056903eeae03e52b0c26?size=30"><a href="/users/24">myname</a>
</li>
<li>
<img alt="myname" class="gravatar" src="http://gravatar.com/avatar/40dcd4dfe68e90cf43e34d3f07e5abd0?size=30"><a href="/users/25">myname</a>
</li>
<li>
<img alt="myname" class="gravatar" src="http://gravatar.com/avatar/577e997028169041902deacb43614df2?size=30"><a href="/users/26">myname</a>
</li>
<li>
<img alt="myname" class="gravatar" src="http://gravatar.com/avatar/811db3fdda06d362216f68a0980ee11d?size=30"><a href="/users/27">myname</a>
</li>
<li>
<img alt="myname" class="gravatar" src="http://gravatar.com/avatar/b4c3b9d853242df89ab17ec671c4afe2?size=30"><a href="/users/28">myname</a>
</li>
<li>
<img alt="myname" class="gravatar" src="http://gravatar.com/avatar/49a89d651bd578d6f3646d344c6ad096?size=30"><a href="/users/29">myname</a>
</li>
<li>
<img alt="myname" class="gravatar" src="http://gravatar.com/avatar/044d71640c5ed3504e65bb8f3bb2576c?size=30"><a href="/users/30">myname</a>
</li>
</ul>
<div class="pagination">
<span class="previous_page disabled">← Previous</span> <em>1</em> <a rel="next" href="/users?escape=false&page=2">2</a> <a class="next_page" rel="next" href="/users?escape=false&page=2">Next →</a>
</div>
</section><footer><nav class="round"><ul>
<li><a href="/about">About</a></li>
<li><a href="/contact">Contact</a></li>
<li><a href="http://news.railstutorial.org/">News</a></li>
<li><a href="http://www.railstutorial.org/">Rails Tutorial</a></li>
</ul></nav></footer>
</div>
</body>
</html>
# ./spec/controllers/users_controller_spec.rb:161
*deleted, see update below*
これが users_controller.rb です - 役に立ったら、他のファイルの内容を投稿します。
class UsersController < ApplicationController
before_filter :authenticate, :only => [:show, :edit, :update]
before_filter :correct_user, :only => [:edit, :update]
def new
@user = User.new
@title = "Sign up"
end
def show
@user = User.find(params[:id])
@title = @user.name
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
@title = "Sign up"
render 'new'
end
end
def edit
@title = "Edit user"
end
def index
@title = "All users"
@users = User.paginate(:page => params[:page])
end
def show
@title = User.find(params[:id])
@title = @user.name
end
def update
@user = User.find(params[:id])
if @user.update_attributes(params[:user])
flash[:success] = "Profile updated."
redirect_to @user
else
@title = "Edit user"
render 'edit'
end
end
private
def authenticate
deny_access unless signed_in?
end
def correct_user
@user = User.find(params[:id])
redirect_to(root_path) unless current_user?(@user)
end
end
更新: users_controller.rb を修正し、ほとんどの問題を修正しました。
def show
@user = User.find(params[:id]) # this was incorrectly written as @title = User.find(params[:id])
@title = @user.name
end
ただし、まだ大きな HTML エラー (#7) があります。response.should have_selector("a", :href => "/users?page=2", :content => "2")
仕様の行を からに変更しようとしましたresponse.should have_selector("a", :href => "/users?escape=false&poge=2", :content => "2")
が、まだ同じ問題が発生しています。
users_controller_spec.rb ファイルは次のとおりです。
require 'spec_helper'
describe UsersController do
render_views
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
it "should have a name field" do
get :new
response.should have_selector("input[name='user[name]'][type='text']")
end
it "should have an email field" do
get :new
response.should have_selector("input [name='user[email]'][type='text']")
end
it "should have a password field" do
get :new
response.should have_selector("input [name='user[password]'][type='password']")
end
it "should have a password confirmation field" do
get :new
response.should have_selector("input [name='user[password_confirmation]'][type='password']")
end
end
describe "GET 'show'" do
before(:each) do
@user = Factory(: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
puts @user.inspect
puts assigns(:user).inspect
assigns(:user).should == @user
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_selector("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
describe "POST 'create'" do
describe "failure" do
before(:each) do
@attr = { :name => "", :email => "", :password => "", :password_confirmation => ""}
end
it "should not create a user" do
lambda do
post :create, :user => @attr
end.should_not change (User, :count)
end
it "should have the right title" do
post :create, :user => @attr
response.should have_selector("title", :content => "Sign up")
end
it "should render the 'new' page" do
post :create, :user => @attr
response.should render_template('new')
end
end
describe "success" do
before(:each) do
@attr = {:name => "New User", :email => "user@example.com", :password => "foobar", :password_confirmation => "foobar"}
end
it "should create a user" do
lambda do
post :create, :user => @attr
end.should change(User, :count).by(1)
end
it "should redirect to the user show page" do
post :create, :user => @attr
response.should redirect_to(user_path(assigns(:user)))
end
it "should have a welcome message" do
post :create, :user => @attr
flash[:success].should =~ /welcome to the sample app/i
end
it "should sign the user in" do
post :create, :user => @attr
controller.should be_signed_in
end
end
end
describe "GET 'index'" do
describe "for non-signed-in users" do
it "should deny access" do
get :index
response.should redirect_to(signin_path)
flash[:notice].should =~ /sign in/i
end
end
describe "for signed-in users" do
before(:each) do
@user = test_sign_in(Factory(:user))
second = Factory(:user, :email => "another@example.com")
third = Factory(:user, :email => "another@example.net")
@users = [@user, second, third]
30.times do
@users << Factory(:user, :email => Factory.next(:email))
end
end
it "should be successful" do
get :index
response.should be_success
end
it "should have the right title" do
get :index
response.should have_selector("title", :content => "All users")
end
it "should have an element for each user" do
get :index
@users[0..2].each do |user|
response.should have_selector("li", :content => user.name)
end
end
it "should paginate users" do
get :index
response.should have_selector("div.pagination")
response.should have_selector("span.disabled", :content => "Previous")
response.should have_selector("a", :href => "/users?escape=false&poge=2", :content => "2")
response.should have_selector("a", :href => "/users?escape=false&page=2", :content => "Next")
end
end
describe "GET 'edit'" do
before(:each) do
@user = Factory(:user)
test_sign_in(@user)
end
it "should be successful" do
get :edit, :id => @user
response.should be_success
end
it "should have the right title" do
get :edit, :id => @user
response.should have_selector("title", :content => "Edit user")
end
it "should have a link to change the Gravatar" do
get :edit, :id => @user
gravatar_url = "http://gravatar.com/emails"
response.should have_selector("a", :href => gravatar_url, :content => "change")
end
end
describe "PUT 'update'" do
before(:each) do
@user = Factory(:user)
test_sign_in(@user)
end
describe "failure" do
before(:each) do
@attr = {:email => "", :name => "", :password => "", :password_confirmation => ""}
end
it "should render the 'edit' page" do
put :update, :id => @user, :user => @attr
response.should render_template('edit')
end
it "should have the right title" do
put :update, :id => @user, :user => @attr
response.should have_selector("title", :content => "Edit user")
end
end
describe "success" do
before(:each) do
@attr = {:name => "New Name", :email => "user@example.org", :password => "barbaz", :password_confirmation => "barbaz"}
end
it "should change the user's attributes" do
put :update, :id => @user, :user => @attr
@user.reload
@user.name.should == @attr[:name]
@user.email.should == @attr[:email]
end
it "should redirect to the user show page" do
put :update, :id => @user, :user => @attr
response.should redirect_to(user_path(@user))
end
it "should have a flash message" do
put :update, :id => @user, :user => @attr
flash[:success].should =~ /updated/
end
end
end
describe "authentication of edit/update pages" do
before(:each) do
@user = Factory(:user)
end
describe "for non-signed-in users" do
it "should deny access to 'edit'" do
get :edit, :id => @user
response.should redirect_to(signin_path)
end
it "should deny access to 'update'" do
put :update, :id => @user, :user => {}
:response.should redirect_to(signin_path)
end
end
describe "for signed-in users" do
before(:each) do
wrong_user = Factory(:user, :email => "user@example.net")
test_sign_in(wrong_user)
end
it "should require matching users for 'edit'" do
get :edit, :id => @user
response.should redirect_to(root_path)
end
it "should require matching users for 'update'" do
put :update, :id => @user, :user => {}
response.should redirect_to(root_path)
end
end
end
end
end