Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何在minitest中为正确的用户创建测试?_Ruby On Rails_Unit Testing_Ruby On Rails 4_Rspec_Minitest - Fatal编程技术网

Ruby on rails 如何在minitest中为正确的用户创建测试?

Ruby on rails 如何在minitest中为正确的用户创建测试?,ruby-on-rails,unit-testing,ruby-on-rails-4,rspec,minitest,Ruby On Rails,Unit Testing,Ruby On Rails 4,Rspec,Minitest,我的“获取编辑”和“销毁测试”一直失败,我不明白为什么。我认为这与控制器中正确的用户方法有关 对于destroy方法,它说contest.count没有以-1改变。编辑方法测试只是转储所有要转储的内容。(没有真正的错误消息) 有人知道我该怎么解决这个问题吗 require "test_helper" describe ContestsController do let(:user) { users :default } let(:contest) { contests :one }

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

对于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 :contests, through: :submissions

  has_one :contest

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

如果您在操作:更正用户之前注释行
,您的测试是否通过:[编辑,:更新,:销毁]
`是的,由于某种奇怪的原因,“更新竞赛”在这样做之后失败了。它在之前通过,现在给出了重定向不再工作的错误。似乎您的
更新竞争
只是检查了重定向,而不是更新本身。由于
重定向
是在您刚才评论的
正确的用户
方法中实现的,
更新
现在不会重定向到任何地方。
竞赛
是否真的属于
用户
?也就是说,你能测试一下expect(user.contestics)吗?要包括(contest)?我得到了类xxx的未定义方法“include”。当我尝试的时候。竞赛通过提交属于用户,也作为管理员。我将在上面发布模型代码。
  has_many :submissions
  has_many :contests, through: :submissions

  has_one :contest