jeudi 28 mai 2015

Call method on variable defined in :let clause in Rspec before all test cases?

I'm working on a chess program and trying to write tests for the Board class. The top of the spec file contained the following code:

describe Board do
    let(:board)       { Board.new }
    let(:empty_board) { Board.new(empty=true) }
    ...
end

However, I read that having boolean flags for methods is a code smell because it signifies that the method is responsible for more than one thing. So, I refactored the logic in the initialize method out into two methods in the board class: create_default_board which initializes the contents of the board to the default configuration, and create_empty_board.

In the spec file, however, I can't figure out how to call these methods on board and empty_board, respectively, before the individual tests are run without having to do so within each describe block. Is there a way around this?

Aucun commentaire:

Enregistrer un commentaire