错误:
“User.count”未更改-1。预期:实际人数:2人
当我试用它的时候,它确实可以在应用程序上工作。但是,当我尝试测试它的时候,它会显示这个错误:
用户控制器测试
test "should destroy user" do
# Log in as an admin user
post login_path, params: { session: { email: "example@railstutorial.org", password: 'foobar', admin: true } }
# Set the user variable explicitly
@user = users(:manon)
assert_difference("User.count", -1) do
delete user_url(@user)
end
assert_redirected_to users_url
# Verify that the user was actually deleted from the database
assert_nil User.find_by(id: @user.id)
end
用户控制器
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
# GET /users or /users.json
def index
@users = User.all
end
# GET /users/1 or /users/1.json
def show
@user = User.find(params[:id])
# debugger
end
# GET /users/new
def new
@user = User.new
end
# GET /users/1/edit
def edit
end
# POST /users or /users.json
def create
@user = User.new(user_params)
respond_to do |format|
if @user.save
log_in @user
format.html { redirect_to user_url(@user), notice: "Welcome to the Sample App" }
format.json { render :show, status: :created, location: @user }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
def edit
@user = User.find(params[:id])
end
# def update
# @user = User.find(params[:id])
# if @user.update(user_params)
# # Handle a successful update.
# else
# render 'edit'
# end
# end
# PATCH/PUT /users/1 or /users/1.json
def update
if @user.update(user_params)
flash[:success] = "Profile Updated"
redirect_to user_url(@user)
else
flash.now[:error] = "There was an error updating your profile."
render :edit, status: :unprocessable_entity
end
end
# DELETE /users/1 or /users/1.json
def destroy
User.find(params[:id]).destroy
flash[:success] = "User deleted"
redirect_to users_url
end
#show micropost for the user
def show
@user = User.find(params[:id])
@micropost = @user.microposts.first
end
private
# Use callbacks to share common setup or constraints between actions.
def set_user
@user = User.find(params[:id])
end
# Only allow a list of trusted parameters through.
def user_params
params.require(:user).permit(:name, :email, :password, :password_confirmation)
end
# Before filters
# Confirms a logged-in user.
def logged_in_user
unless logged_in?
store_location
flash[:danger] = "Please log in."
redirect_to login_url
end
end
# Confirms the correct user.
def correct_user
@user = User.find(params[:id])
redirect_to(root_url) unless current_user?(@user)
end
# Confirms an admin user.
def admin_user
redirect_to(root_url) unless current_user.admin?
end
end
会话助手
module SessionsHelper
# Logs in the given user.
def log_in(user)
session[:user_id] = user.id
end
# Remembers a user in a persistent session.
def remember(user)
user.remember
cookies.permanent.signed[:user_id] = user.id
cookies.permanent[:remember_token] = user.remember_token
end
# Returns the user corresponding to the remember token cookie.
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
# Returns true if the given user is the current user.
def current_user?(user)
user && user == current_user
end
# Returns true if the user is logged in, false otherwise.
def logged_in?
!current_user.nil?
end
# Forgets a persistent session.
def forget(user)
user.forget
cookies.delete(:user_id)
cookies.delete(:remember_token)
end
# Logs out the current user.
def log_out
forget(current_user)
session.delete(:user_id)
@current_user
end
# Redirects to stored location (or to the default).
def redirect_back_or(default)
redirect_to(session[:forwarding_url] || default)
session.delete(:forwarding_url)
end
# Stores the URL trying to be accessed.
def store_location
session[:forwarding_url] = request.url if request.get?
end
end
1条答案
按热度按时间p8h8hvxi1#
您是否尝试过: