jeudi 24 septembre 2015

How to create test for correct_user in minitest?

My "gets edit" and "destroys tests" keep failing and I do not understand why. I think it has to do with the correct_user method in my controller.

For the destroy method it says that the contest.count did not change by -1. The edit method test just dumps everything there is to dump. (no real error message)

Does anybody have an idea how I can fix this?

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

Controller below:

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

Aucun commentaire:

Enregistrer un commentaire