【问题标题】:How to create test for correct_user in minitest?如何在 minitest 中为正确用户创建测试?
【发布时间】:2015-09-24 19:26:33
【问题描述】:

我的“获取编辑”和“破坏测试”一直失败,我不明白为什么。我认为这与我控制器中的正确用户方法有关。

对于destroy 方法,它说competition.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

【问题讨论】:

  • 如果您对before_action :correct_user, only: [:edit, :update, :destroy] 发表评论,您的测试是否通过? `
  • 是的,由于某些奇怪的原因,“更新竞赛”在这样做之后失败了。它之前通过,现在给出重定向不再起作用的错误。
  • 您的updates contests 似乎只是检查了重定向而不是更新本身。由于redirect 是在您刚刚评论的correct_user 方法中实现的,update 现在不会重定向到任何地方。
  • contest 真的属于user 吗? IE。你能测试一下expect(user.contests).to include(contest)吗?
  • 我得到类 xxx 的未定义方法“包含”。当我尝试的时候。比赛通过提交属于用户,也属于管理员。我将在上面发布模型代码。

标签: ruby-on-rails unit-testing ruby-on-rails-4 rspec minitest


【解决方案1】:

似乎我没有正确设置我的夹具关系。我有很多通过提交的关系,而我的固定装置没有。现在它正在工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-11
    • 1970-01-01
    相关资源
    最近更新 更多