Search code examples
ruby-on-railsrubyrspecfactory-botrspec-rails

DRYing my RSPEC Test for a Rook Chess Piece: Rook.valid_move?


I'm looking to DRY up my code in order to test a valid move for a Rook moving in vertically from the first row of the chess board (x_position = 0).

Here's my current brain bender of a challenge. The following block of code cycles through all the squares on the board starting at the x_position(0) and iterates the y_position(0..7) and finally iterates each x_poisition up to 7.

My current spec test code:

require 'rails_helper'

RSpec.describe Rook, type: :model do
  describe "#valid_move?" do
    it "returns true if move is valid vertically" do
      game = FactoryGirl.create(:game)
      rook = FactoryGirl.create(:rook, x_position: 0, y_position: 0, game: game)
      (0..7).each {|i| (0..7).each {|j| expect(rook.valid_move?(i,j)).to eq(true)}}
    end

What I'm after with this code is to, instead of FactoryGirl starting the rook at (0,0), use the loops to have FatroyGirl start the rook at (0,0) and iterate the y_position(1..7) then increment the x_position by 1 and iterate the y_position(1..7) again until (7,7) has been tested.


Solution

  • I'm by no means an expert, but it looks like you are creating a piece at position (0,0)

    Then you test if moving from (0,0) to every location on the board and assert that all locations should be valid.

    I'd suggest unwrapping the loops into the begin/end form and putting in some print statements so that you can understand what is going on.

    I think you want more of a:

    (0..7).each {|i| rook.move_to(?,?) ; (i..7).each {|j| expect(rook.valid_move?(i,j)).to eq(true)}}
    

    So basically you want to do things in two stages. First set the rook to a location, then test if it can move to other valid locations. And possibly also test attempts to move to invalid locations.