Organizing Your Test in Arrange, Act, and Assert Blocks
Tests are about assertions (ASSERT). For an assertion to be possible, we need to take an action on the code we are asserting (ACT). Sometimes, to take an action, we need to create the context to enable the action (ARRANGE). Organizing tests in arrange, act, and assert blocks helps keep the tests clean and easy to read.
For example, let's look at fizzbuzz
:
//Arrange block (aka Given) – create all necessary preconditions var fizzBuzzer = new FizzBuzzer() //Act block (aka When) – execute the subject under test var fizzBuzzed = fizzBuzzer.FizzBuzz(1) //Assert block (aka Then) – assert the expected results have occurred assert fizzBuzzed == "1"
However, we think that less is more when we are trying to understand the meaning of some code. We are not...