Rspec 有问题。获取路由错误
Having trouble with Rspec. Getting routing error
我正在开发一个构建国际象棋应用程序的小组项目,并试图了解为什么在 运行 此测试时出现路由错误。
我正在尝试在我的作品控制器中测试更新操作,以确保它在移动作品后重定向。请记住,我是测试新手,我猜我写这篇文章的方式有问题。真的卡住了,可以解释我所缺少的东西。
控制器:
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
您在 put :update, params: {x_position: 1, y_position: 1}
错过了 id
?
你可能想要这样的东西:
put :update, id: piece.id, game_id: game.id, piece: {x_position: 1, y_position: 1}
我正在开发一个构建国际象棋应用程序的小组项目,并试图了解为什么在 运行 此测试时出现路由错误。 我正在尝试在我的作品控制器中测试更新操作,以确保它在移动作品后重定向。请记住,我是测试新手,我猜我写这篇文章的方式有问题。真的卡住了,可以解释我所缺少的东西。
控制器:
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
您在 put :update, params: {x_position: 1, y_position: 1}
错过了 id
?
你可能想要这样的东西:
put :update, id: piece.id, game_id: game.id, piece: {x_position: 1, y_position: 1}