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

后端 未结 14 2409
不思量自难忘°
不思量自难忘° 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:59

    It could also be specified as Arrange-Assume-Act-Assert.

    There is a technical handle for this in NUnit, as in the example here: http://nunit.org/index.php?p=theory&r=2.5.7

    0 讨论(0)
  • 2020-11-30 21:00

    Here's an example.

    public void testEncompass() throws Exception {
        Range range = new Range(0, 5);
        assertFalse(range.includes(7));
        range.encompass(7);
        assertTrue(range.includes(7));
    }
    

    It could be that I wrote Range.includes() to simply return true. I didn't, but I can imagine that I might have. Or I could have written it wrong in any number of other ways. I would hope and expect that with TDD I actually got it right - that includes() just works - but maybe I didn't. So the first assertion is a sanity check, to ensure that the second assertion is really meaningful.

    Read by itself, assertTrue(range.includes(7)); is saying: "assert that the modified range includes 7". Read in the context of the first assertion, it's saying: "assert that invoking encompass() causes it to include 7. And since encompass is the unit we're testing, I think that's of some (small) value.

    I'm accepting my own answer; a lot of the others misconstrued my question to be about testing the setup. I think this is slightly different.

    0 讨论(0)
提交回复
热议问题