0

I am writing test for my class, the following test does not pass.

I expect my "create_board" method to create 64 squares. when I ran my test, I received the message:

  • Expected: 64 times with any arguments
  • Received: 65 times

I tried to replace n with another number and I kept receiving a message with n+1 times, like this:

  • Expected: n times with any arguments
  • Received: n+1 times

Anyone can tell me what is wrong with my code, please? Thank you very much. Below is my code:

class Board
    def create_board 
        coor_collection = []
        (1..8).to_a.repeated_permutation(2) { |per| coor_collection << per}
        coor_collection.map do |coordinator|
            Square.new(coordinator)
        end
    end
end 

describe Board do 
    subject(:board) { described_class.new }
    describe "#create_board" do 
        it "create 64 square-objects" do 
            expect(Square).to receive(:new).exactly(64).times
            board.create_board
        end
    end
end

1 Answers1

0

Your code works perfectly for me when I run it locally, so you must have something else that you're not showing us. Maybe a before block, or some hook in your model or something somewhere that's calling that extra Square.new.

smathy
  • 26,283
  • 5
  • 48
  • 68
  • 1
    @samthy Thank you very much, I've just figured out the problem, in my Board class, my initialize function have an instance variable "square_collection" and I set it to equal to my method "create_board". – Thảo Nguyễn Jun 01 '23 at 06:26