測試我一直收到三個錯誤,我不知道爲什麼。我會盡我所能地跟隨教程,並且我已經通過第9章學習了錯誤,並且我還沒有看到任何錯誤。運行bundle exec rake test
。我得到了以下三個錯誤:Ruby on Rails的教程第3版,第9章:失敗的users_edit_test.rb
FAIL["test_unsuccessful_edit", UsersEditTest, 2015-08-22 09:09:06 +0900]
test_unsuccessful_edit#UsersEditTest (1440202146.96s)
expecting <"users/edit"> but rendering with <[]>
test/integration/users_edit_test.rb:12:in `block in <class:UsersEditTest>'
FAIL["test_successful_edit", UsersEditTest, 2015-08-22 09:09:06 +0900]
test_successful_edit#UsersEditTest (1440202146.97s)
expecting <"users/edit"> but rendering with <[]>
test/integration/users_edit_test.rb:25:in `block in <class:UsersEditTest>'
FAIL["test_successful_edit_with_friendly_forwarding", UsersEditTest, 2015-08-22 09:09:07 +0900]
test_successful_edit_with_friendly_forwarding#UsersEditTest (1440202147.07s)
Expected response to be a redirect to <http://www.example.com/users/762146111/edit> but was a redirect to <http://www.example.com/>.
Expected "http://www.example.com/users/762146111/edit" to be === "http://www.example.com/".
test/integration/users_edit_test.rb:43:in `block in <class:UsersEditTest>'
正如你所看到的,我的錯誤,可在測試/集成/ users_edit_test找到。我的兩個測試都以相同的方式失敗,並顯示以下消息:expecting <"users/edit"> but rendering with <[]>
。第12行和25具有相同的代碼:
assert_template 'users/edit'
線43具有下面的代碼:
assert_redirected_to edit_user_path(@user)
我有一個應該去用戶的一個鏈接_header.html.erb文件編輯頁面,但它只是重定向到索引。我有一個文件app/views/users/edit.html.erb,所以我不知道它爲什麼沒有顯示出來。下面是設置鏈接代碼:
<li><%= link_to "Settings", edit_user_path(current_user) %></li>
這是我爲我的users_controller.rb文件的代碼。我懷疑問題是我的correct_user
方法沒有按照它應該的那樣發射。 (順便說一句,我曾嘗試註釋掉「編輯」方法中的代碼,但問題依然存在。)
class UsersController < ApplicationController
before_action :logged_in_user, only: [:index, :edit, :update, :destroy]
before_action :correct_user, only: [:edit, :update]
before_action :admin_user, only: :destroy
def index
@users = User.paginate(page: params[:page])
end
def show
@user = User.find(params[:id])
# debugger
end
def new
@user = User.new
end
def create
@user = User.new(user_params)
if @user.save
log_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
if @user.update_attributes(user_params)
flash[:success] = "Profile updated"
redirect_to @user
else
render 'edit'
end
end
def destroy
User.find(params[:id].destroy)
flash[:success] = "User deleted"
redirect_to users_url
end
private
def user_params
params.require(:user).permit(:name, :email, :password, :password_confirmation)
end
def logged_in_user
unless logged_in?
store_location
flash[:danger] = "Please log in."
redirect_to login_url
end
end
def correct_user
@user = User.find(params[:id])
# redirect_to(root_url) unless @user == current_user
redirect_to(root_url) unless @user == current_user?(@user)
end
def admin_user
redirect_to(root_url) unless current_user.admin?
end
end
在app /傭工/ sesions_helper.rb我有兩種方法,current_user
和current_user?
def current_user?(user)
user == current_user
end
def current_user
if(user_id = session[:user_id])
@current_user ||= User.find_by(id: user_id)
elsif (user_id = cookies.signed[:user_id])
user = User.find_by(id: user_id)
if user && user.authenticated?(cookies[:remember_token])
log_in user
@current_user = user
end
end
end
下面是配置/ routes文件:
root 'static_pages#home'
get 'help' => 'static_pages#help'
get 'about' => 'static_pages#about'
get 'contact' => 'static_pages#contact'
get 'signup' => 'users#new'
get 'login' => 'sessions#new'
post 'login' => 'sessions#create'
delete 'logout' => 'sessions#destroy'
resources :users
我懷疑回答我的問題是超級容易,但我不知道什麼是錯的。這裏是users_edit_test.rb文件
require 'test_helper'
class UsersEditTest < ActionDispatch::IntegrationTest
def setup
@user = users(:michael)
end
test "unsuccessful edit" do
# log_in_as(@user)
get edit_user_path(@user)
log_in_as(@user)
assert_template 'users/edit'
patch user_path(@user), user: { name: '',
email: '[email protected]',
password: 'foo',
password_confirmation: 'bar' }
assert_template 'users/edit'
end
test "successful edit" do
# log_in_as(@user)
get edit_user_path(@user)
log_in_as(@user)
assert_template 'users/edit'
name = "Foo Bar"
email = "[email protected]"
patch user_path(@user), user: { name: name,
email: email,
password: '',
password_confirmation: '' }
assert_not flash.empty?
assert_redirected_to @user
@user.reload
assert_equal @user.name, name
assert_equal @user.email, email
end
test "successful edit with friendly forwarding" do
log_in_as(@user)
get edit_user_path(@user)
# log_in_as(@user)
assert_redirected_to edit_user_path(@user)
name = "Foo Bar"
email = "[email protected]"
patch user_path(@user), user: { name: name,
email: email,
password: 'foobar',
password_confirmation: 'foobar' }
assert_not flash.empty?
assert_redirected_to @user
@user.reload
assert_equal @user.name, name
assert_equal @user.email, email
end
end
請更新您的問題。 –
爲什麼編輯方法中的@user被註釋掉了? – engineerDave
@engineerDave因爲在編輯方法之前運行的correct_user方法在使用編輯或更新方法時執行相同的任務。 –