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

时间:2017-03-11 14:36:10

标签: ruby-on-rails ruby rspec tdd

我正在建立一个建立国际象棋应用程序的小组项目。试图理解为什么我在运行此测试时遇到路由错误。 我正在尝试在我的棋子控制器中测试更新操作,以确保一旦移动一块它就重定向。请记住,我是测试的新手,并且我猜测我是如何编写这个问题的。真的卡住了,可以用我对失踪的解释。

控制器:

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

规格:

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

路线:

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

1 个答案:

答案 0 :(得分:1)

id上遗失了put :update, params: {x_position: 1, y_position: 1}

您可能需要以下内容:

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