如何在 minitest 中为 correct_user 创建测试?
How to create test for correct_user in minitest?
我的 "gets edit" 和 "destroys tests" 总是失败,我不明白为什么。我认为这与我的控制器中的 correct_user 方法有关。
对于 destroy 方法,它表示 contest.count 没有改变 -1。编辑方法测试只是转储要转储的所有内容。 (没有真正的错误消息)
有人知道我该如何解决这个问题吗?
require "test_helper"
describe ContestsController do
let(:user) { users :default }
let(:contest) { contests :one }
it "gets index" do
get :index
value(response).must_be :success?
value(assigns(:contests)).wont_be :nil?
end
describe "gets new" do
it "redirects to login_path" do
session[:user_id] = nil
get :new
assert_redirected_to new_user_session_path
end
it "requires authentication" do
sign_in users :default
get :new
value(response).must_be :success?
end
end
it "creates contest" do
sign_in users :default
expect {
post :create, contest: { name: "test", admin_id: 1 }
}.must_change "Contest.count"
must_redirect_to contest_path(assigns(:contest))
end
it "shows contest" do
get :show, id: contest
value(response).must_be :success?
end
it "gets edit" do
sign_in users :default
get :edit, id: contest
value(response).must_be :success?
end
it "updates contest" do
sign_in users :default
put :update, id: contest, contest: { name: "bier" }
must_redirect_to contests_path
end
it "destroys contest" do
sign_in users :default
expect {
delete :destroy, id: contest
}.must_change "Contest.count", -1
must_redirect_to contests_path
end
end
下面的控制器:
class ContestsController < ApplicationController
before_action :set_contest, only: [:show, :edit, :update, :destroy]
# the current user can only edit, update or destroy if the id of the pin matches the id the user is linked with.
before_action :correct_user, only: [:edit, :update, :destroy]
# the user has to authenticate for every action except index and show.
before_action :authenticate_user!, except: [:index, :show]
respond_to :html
def index
@title = t('contests.index.title')
set_meta_tags keywords: %w[leaderboard contest win],
description: "View all the #{Settings.appname} leaderboards now!"
@contests = Contest.all
respond_with(@contests)
end
def show
@title = t('contests.show.title')
#set_meta_tags keywords: %w[],
#description: ""
respond_with(@contest)
end
def new
@title = t('contests.new.title')
#set_meta_tags keywords: %w[],
#description: ""
@contest = current_user.contests.new
respond_with(@contest)
end
def edit
@title = t('contests.edit.title')
#set_meta_tags keywords: %w[],
#description: ""
end
def create
@title = t('contests.create.title')
#set_meta_tags keywords: %w[],
#description: ""
@contest = current_user.contests.new(contest_params)
@contest.admin_id = current_user.id
@contest.save
respond_with(@contest)
end
def update
@title = t('contests.update.title')
#set_meta_tags keywords: %w[],
#description: ""
@contest.update(contest_params)
respond_with(@contest)
end
def destroy
@title = t('contests.destroy.title')
#set_meta_tags keywords: %w[],
#description: ""
@contest.destroy
respond_with(@contest)
end
private
def set_contest
@contest = Contest.find(params[:id])
end
def contest_params
params.require(:contest).permit(:name, :description)
end
def correct_user
@contest = current_user.contests.find_by(id: params[:id])
redirect_to contests_path, notice: t('controller.correct_user') if @contest.nil?
end
end
模特大赛
has_many :submissions
has_many :users, through: :submissions
belongs_to :admin, class_name: 'User', foreign_key: 'admin_id'
模范用户
has_many :submissions
has_many :contests, through: :submissions
has_one :contest
看来我的夹具关系设置不正确。我有很多通过提交的关系,而我的固定装置没有。现在可以使用了。
我的 "gets edit" 和 "destroys tests" 总是失败,我不明白为什么。我认为这与我的控制器中的 correct_user 方法有关。
对于 destroy 方法,它表示 contest.count 没有改变 -1。编辑方法测试只是转储要转储的所有内容。 (没有真正的错误消息)
有人知道我该如何解决这个问题吗?
require "test_helper"
describe ContestsController do
let(:user) { users :default }
let(:contest) { contests :one }
it "gets index" do
get :index
value(response).must_be :success?
value(assigns(:contests)).wont_be :nil?
end
describe "gets new" do
it "redirects to login_path" do
session[:user_id] = nil
get :new
assert_redirected_to new_user_session_path
end
it "requires authentication" do
sign_in users :default
get :new
value(response).must_be :success?
end
end
it "creates contest" do
sign_in users :default
expect {
post :create, contest: { name: "test", admin_id: 1 }
}.must_change "Contest.count"
must_redirect_to contest_path(assigns(:contest))
end
it "shows contest" do
get :show, id: contest
value(response).must_be :success?
end
it "gets edit" do
sign_in users :default
get :edit, id: contest
value(response).must_be :success?
end
it "updates contest" do
sign_in users :default
put :update, id: contest, contest: { name: "bier" }
must_redirect_to contests_path
end
it "destroys contest" do
sign_in users :default
expect {
delete :destroy, id: contest
}.must_change "Contest.count", -1
must_redirect_to contests_path
end
end
下面的控制器:
class ContestsController < ApplicationController
before_action :set_contest, only: [:show, :edit, :update, :destroy]
# the current user can only edit, update or destroy if the id of the pin matches the id the user is linked with.
before_action :correct_user, only: [:edit, :update, :destroy]
# the user has to authenticate for every action except index and show.
before_action :authenticate_user!, except: [:index, :show]
respond_to :html
def index
@title = t('contests.index.title')
set_meta_tags keywords: %w[leaderboard contest win],
description: "View all the #{Settings.appname} leaderboards now!"
@contests = Contest.all
respond_with(@contests)
end
def show
@title = t('contests.show.title')
#set_meta_tags keywords: %w[],
#description: ""
respond_with(@contest)
end
def new
@title = t('contests.new.title')
#set_meta_tags keywords: %w[],
#description: ""
@contest = current_user.contests.new
respond_with(@contest)
end
def edit
@title = t('contests.edit.title')
#set_meta_tags keywords: %w[],
#description: ""
end
def create
@title = t('contests.create.title')
#set_meta_tags keywords: %w[],
#description: ""
@contest = current_user.contests.new(contest_params)
@contest.admin_id = current_user.id
@contest.save
respond_with(@contest)
end
def update
@title = t('contests.update.title')
#set_meta_tags keywords: %w[],
#description: ""
@contest.update(contest_params)
respond_with(@contest)
end
def destroy
@title = t('contests.destroy.title')
#set_meta_tags keywords: %w[],
#description: ""
@contest.destroy
respond_with(@contest)
end
private
def set_contest
@contest = Contest.find(params[:id])
end
def contest_params
params.require(:contest).permit(:name, :description)
end
def correct_user
@contest = current_user.contests.find_by(id: params[:id])
redirect_to contests_path, notice: t('controller.correct_user') if @contest.nil?
end
end
模特大赛
has_many :submissions
has_many :users, through: :submissions
belongs_to :admin, class_name: 'User', foreign_key: 'admin_id'
模范用户
has_many :submissions
has_many :contests, through: :submissions
has_one :contest
看来我的夹具关系设置不正确。我有很多通过提交的关系,而我的固定装置没有。现在可以使用了。