Brandon Elder Brandon Elder - 9 months ago 23
Ruby Question

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

Answer Source

You're missing id on put :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}