rspecstubrspec-expectations

My rspec test does not pass, I expect my method to receive message expectation n times, but it keeps getting n+1 times


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:

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

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

Solution

  • 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.