Should it be “Arrange-Assert-Act-Assert”?

后端 未结 14 2547
不思量自难忘°
不思量自难忘° 2020-11-30 19:53

Regarding the classic test pattern of Arrange-Act-Assert, I frequently find myself adding a counter-assertion that precedes Act. This way I know that the passing assertion

14条回答
  •  孤城傲影
    2020-11-30 20:36

    I don't use that pattern, because I think doing something like:

    Arrange
    Assert-Not
    Act
    Assert
    

    May be pointless, because supposedly you know your Arrange part works correctly, which means that whatever is in the Arrange part must be tested aswell or be simple enough to not need tests.

    Using your answer's example:

    public void testEncompass() throws Exception {
        Range range = new Range(0, 5);
        assertFalse(range.includes(7)); // <-- Pointless and against DRY if there 
                                        // are unit tests for Range(int, int)
        range.encompass(7);
        assertTrue(range.includes(7));
    }
    

提交回复
热议问题