简体   繁体   English

Rspec遇到问题。 获取路由错误

[英]Having trouble with Rspec. Getting routing error

I'm working on a group project building a chess application & trying to understand why I am getting a routing error when running this test. 我正在一个构建国际象棋应用程序的小组项目中,试图理解为什么在运行此测试时出现路由错误。 I am trying to test the update action in my pieces controller, to make sure that it redirects once a piece is moved. 我正在尝试在我的棋子控制器中测试更新操作,以确保一旦棋子被移动,它就会重定向。 Bare in mind I am new to testing and am guessing that there is a problem with how I've written this one. 记不清了,我是测试的新手,并且猜测我编写此书的方式存在问题。 Really stuck and could use an explanation of what I am missing. 真的卡住了,可以使用我所缺少的解释。

controller: 控制器:

class PiecesController < ApplicationController
  before_action :authenticate_user!, only: [:show, :update]

  def show
    #show the board again
    @game = Game.find(params[:id])
    @pieces = @game.pieces
  end

  def update
    @piece = Piece.find(params[:id])
    @game = @piece.game

    if @piece.update_attributes(piece_params)
      redirect_to game_path(@game)
    end 
  end

  private

  def piece_params
    params.require(:piece).permit(:x_position, :y_position)
  end
end

spec: 规格:

require 'rails_helper'

RSpec.describe PiecesController, type: :controller do
  describe 'update action' do
    it 'should redirect to game path when piece is moved' do
      user1 = FactoryGirl.create(:user)
      game = FactoryGirl.create(:game, white_user_id: user1.id )
      piece = FactoryGirl.create(:piece, game: game, user_id: user1.id, x_position: 0, y_position: 0)

      put :update, params: {x_position: 1, y_position: 1}

      expect(response).to redirect_to game_path(game)
    end
  end
end

routes: 路线:

Rails.application.routes.draw do
  devise_for :users
  root 'static_pages#index'
  resources :games do
    member do
      patch :join
    end
    resources :pieces, only: [:show, :update]
  end
end

You're missing id on put :update, params: {x_position: 1, y_position: 1} ? 你错过了idput :update, params: {x_position: 1, y_position: 1}

You probably want something like: 您可能想要类似的东西:

put :update, id: piece.id, game_id: game.id, piece: {x_position: 1, y_position: 1}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM