given-when-then is a commonly used style of specifying system behaviour in which your tests are split into three sections.
- Given is the section that lays out the pre-conditions for the test, ie whatever state you're assuming the world to be in before you start.
- The When clause performs the action being tested.
- The Then statement checks that the post condition holds. This is usually in the form of asserting values or checking interaction with mocks.
It's not always that case that you need to have three sections in the code of every test. For example your given section might be covered by a common setUp
method. I think it's a good idea to follow the pattern and split up the different sections because it allows you to clearly see the wood from the trees.
Read full article from InsightfulLogic
No comments:
Post a Comment