我正在研究 Michael Hartl Rails 3.2,我已经被这些问题困扰了几个小时。我无法弄清楚我错过了什么,因为我已经检查了 7、8、9 中的所有代码以确保它是准确的。更新/编辑页面一直在工作,直到我在我的用户控件的私有部分中添加。
由于我添加了这些,当我手动测试页面并点击设置时,它直接进入登录页面。似乎它没有接受除非已签名?并将其从用户控制器页面重定向到签名页面。我的测试失败是有道理的,因为编辑页面没有出现以登录。任何想法或建议将不胜感激。如果您需要查看其他页面,请告诉我。
错误消息 - 我收到 9 个错误,如下所示
Failures:
1) UserPages edit page
Failure/Error: it { should have_selector('h1', text: "Update your profile") }
expected css "h1" with text "Update your profile" to return something
# ./spec/requests/user_pages_spec.rb:73:in `block (4 levels) in <top (required)>'
2) UserPages edit page
Failure/Error: it { should have_link('change', href: 'http://gravatar.com/emails')}
expected link "change" to return something
# ./spec/requests/user_pages_spec.rb:75:in `block (4 levels) in <top (required)>'
3) UserPages edit page
Failure/Error: it { should have_selector('title', text: "Edit user") }
expected css "title" with text "Edit user" to return something
# ./spec/requests/user_pages_spec.rb:74:in `block (4 levels) in <top (required)>'
4) UserPages edit with valid information
Failure/Error: fill_in "Name", with: new_name
Capybara::ElementNotFound:
cannot fill in, no text field, text area or password field with id, name, or label 'Name' found
# (eval):2:in `fill_in'
# ./spec/requests/user_pages_spec.rb:88:in `block (4 levels) in <top (required)>'
5) UserPages edit with valid information
Failure/Error: fill_in "Name", with: new_name
Capybara::ElementNotFound:
cannot fill in, no text field, text area or password field with id, name, or label 'Name' found
# (eval):2:in `fill_in'
# ./spec/requests/user_pages_spec.rb:88:in `block (4 levels) in <top (required)>'
6) UserPages edit with valid information
Failure/Error: fill_in "Name", with: new_name
Capybara::ElementNotFound:
cannot fill in, no text field, text area or password field with id, name, or label 'Name' found
# (eval):2:in `fill_in'
# ./spec/requests/user_pages_spec.rb:88:in `block (4 levels) in <top (required)>'
7) UserPages edit with valid information
Failure/Error: fill_in "Name", with: new_name
Capybara::ElementNotFound:
cannot fill in, no text field, text area or password field with id, name, or label 'Name' found
# (eval):2:in `fill_in'
# ./spec/requests/user_pages_spec.rb:88:in `block (4 levels) in <top (required)>'
8) UserPages edit with valid information
Failure/Error: fill_in "Name", with: new_name
Capybara::ElementNotFound:
cannot fill in, no text field, text area or password field with id, name, or label 'Name' found
# (eval):2:in `fill_in'
# ./spec/requests/user_pages_spec.rb:88:in `block (4 levels) in <top (required)>'
9) UserPages edit with invalid information
Failure/Error: before { click_button "Save changes" }
Capybara::ElementNotFound:
no button with value or id or text 'Save changes' found
# (eval):2:in `click_button'
# ./spec/requests/user_pages_spec.rb:79:in `block (4 levels) in <top (required)>'
这是身份验证规范页面
require 'spec_helper'
describe "AuthenticationPages" do
subject { page }
describe "signin page" do
before { visit signin_path }
it { should have_selector('h1', text: 'Sign in') }
it { should have_selector('title', text: 'Sign in') }
end
describe "signin" do
before { visit signin_path}
describe "with invalid information" do
before { click_button "Sign in" }
it { should have_selector('title', text: 'Sign in') }
it { should have_selector('div.alert.alert-error', text: 'Invalid') }
describe "after visiting another page" do
before { click_link "Home" }
it { should_not have_selector('div.alert.alert-error') }
end
end
describe "with valid information" do
let(:user) { FactoryGirl.create(:user) }
before { sign_in user }
describe "followed by signout" do
before { click_link "Sign out" }
it { should have_link('Sign in')}
end
it { should have_selector('title', text: user.name) }
it { should have_link('Profile', href: user_path(user)) }
it { should have_link('Settings', href: edit_user_path(user)) }
it { should have_link('Sign out', href: signout_path) }
it { should_not have_link('Sign in', href: signin_path) }
end
end
describe "authorization" do
describe "for non-signed-in users" do
let(:user) { FactoryGirl.create(:user) }
describe "in the Users controller" do
describe "visiting the edit page" do
before { visit edit_user_path(user) }
it { should have_selector('title', text: 'Sign in' ) }
end
describe "submitting to the update action" do
before { put user_path(user) }
specify { response.should redirect_to(signin_path) }
end
end
end
end
end
和用户页面规范
require 'spec_helper'
describe "UserPages" do
subject { page }
describe "profile page" do
before { visit user_path(user) }
let(:user) {FactoryGirl.create(:user) }
it { should have_selector('h1', text: user.name)}
it { should have_selector('title', text: user.name) }
end
describe "signup page" do
before {visit signup_path}
it { should have_selector('h1', text: 'Sign up') }
it { should have_selector('title', text: full_title('Sign up'))}
end
describe "signup" do
before { visit signup_path}
let(:submit) { "Create my account"}
describe "with invalid information" do
it "should not create a user" do
expect { click_button submit }.not_to change(User, :count)
end
describe "after submission" do
before { click_button submit }
it { should have_selector('title', text: 'Sign up') }
it { should have_content('error') }
end
end
describe "with valid information" do
before do
fill_in "Name", with: "Exmaple User"
fill_in "Email", with: "user@example.com"
fill_in "Password", with: "foobar"
fill_in "Password confirmation", with: "foobar"
end
it "should create a user" do
expect { click_button submit }.to change(User, :count).by(1)
end
describe "after saving the user" do
before { click_button submit }
let(:user) { User.find_by_email('user@example.com') }
it { should have_selector('title', text: user.name) }
it { should have_selector('div.alert.alert-success', text: 'Welcome') }
it { should have_link('Sign out') }
end
end
end
describe "edit" do
let(:user) { FactoryGirl.create(:user) }
before do
visit signin_path
sign_in user
visit edit_user_path(user)
end
describe "page" do
it { should have_selector('h1', text: "Update your profile") }
it { should have_selector('title', text: "Edit user") }
it { should have_link('change', href: 'http://gravatar.com/emails')}
end
describe "with invalid information" do
before { click_button "Save changes" }
it { should have_content('error') }
end
describe "with valid information" do
let(:new_name) { "New Name"}
let(:new_email) {"new@example.com"}
before do
fill_in "Name", with: new_name
fill_in "Email", with: new_email
fill_in "Password", with: user.password
fill_in "Confirm Password", with: user.password
click_button "Save changes"
end
it { should have_selector('title', text: new_name) }
it { should have_selector('div.alert.alert-success') }
it { should have_link('Sign out', href: signout_path) }
specify { user.reload.name.should == new_name }
specify { user.reload.email.should == new_email }
end
end
end
Utilities 中的 sign_in(user)
def sign_in(user)
fill_in "Email", with: user.email
fill_in "Password", with: user.password
click_button "Sign in"
# Sign in when not using Capybara as well.
cookies[:remember_token] = user.remember_token
结尾
会话助手
module SessionsHelper
def sign_in(user)
cookies.permanent[:remember_token] = user.remember_token
self.current_user = user
end
def signed_in?
!current_user.nil?
end
def current_user=(user)
@current_user = user
end
def current_user
@current_user ||= User.find_by_remember_token(cookies[:remember_token])
end
def current_user?(user)
user == current_user
end
def sign_out
self.current_user = nil
cookies.delete(:remember_token)
end
def redirect_back_or(default)
redirect_to(session[:return_to] || default)
session.delete(:return_to)
end
def store_location
session[:return_to] = request.fullpath
end
end
用户控制器
class UsersController < ApplicationController
before_filter :signed_in_user, only: [:edit, :update]
before_filter :correct_user, only: [:edit, :update]
def show
@user = User.find(params[:id])
end
def new
@user = User.new
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
render 'new'
end
end
def edit
@user = User.find(params[:id])
end
def update
@user = User.find(params[:id])
if @user.update_attributes(params[:user])
sign_in @user
flash[:success] = "Profile updated"
redirect_to @user
else
render 'edit'
end
end
private
def signed_in_user
unless signed_in?
store_location
redirect_to signin_path, notice: "Please sign in."
end
end
def correct_user
@user=User.find(params[:id])
redirect_to(root_path) unless current_user?(@user)
end
end
视图/布局/_header.html.erb
<header class="navbar navbar-fixed-top">
<div class="navbar-inner">
<div class="container">
<%= link_to "sample app", root_path, id: "logo" %>
<nav>
<ul class="nav pull-right">
<li><%= link_to "Home", root_path %></li>
<li><%= link_to "Help", help_path %></li>
<% if signed_in? %>
<li><%= link_to "Users", "#" %></li>
<li id="fat-menu" class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
Account <b class="caret"></b>
</a>
<ul class="dropdown-menu">
<li><%= link_to "Profile", current_user %></li>
<li><%= link_to "Settings", edit_user_path(current_user) %></li>
<li class="divider"></li>
<li>
<%= link_to "Sign out", signout_path, method: "delete" %>
</li>
</ul>
</li>
<% else %>
<li><%= link_to "Sign in", signin_path %></li>
<% end %>
</ul>
</nav>
</div>
</div>
如果您遇到此问题 - 请确保您的 application_controller 中没有此问题。这是一个很小的错误,却给我带来了各种各样的悲痛。再次 - 不要使用此代码......
class ApplicationController < ActionController::Base
protect_from_forgery
include SessionsHelper
private
def current_user
@current_user ||= User.find(session[:user_id]) if session[:user_id]
end
结尾