如何在minitest中为correct_user创建测试?

时间:2015-09-24 19:26:33

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

我的"得到编辑"和#34;破坏测试"继续失败,我不明白为什么。我认为它与我的控制器中的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

1 个答案:

答案 0 :(得分:1)

似乎我没有正确设置夹具关系。我有很多通过提交关系,我的设备没有它。现在它正在发挥作用。