I am unsure where/how to specify the behavior of a mock in a test scenario when using the Given-When-Then syntax. It seems to me both Given and When could be correct.
Consider the following example:
- I want to test the control panel for a car.
- This panel has a start button for the engine and a LED that shows the engine status.
- When the user presses the start button, the engine should be started and the LED should be green or red, depending on whether the engine was started successfully.
- The control panel is the SUT, the engine is mocked.
- In the specific test scenario, we test what happens when the engine fails to start.
Which of the following Given-When-Thens is the more correct one:
This one:
- Given a control panel in its initial state
- And an engine that will fail to start
- When the start button is pressed
- Then the LED will become red
Or this one:
- Given a control panel in its initial state
- When the start button is pressed
- And the engine fails to start
- Then the LED will become red
Personally I feel that the second way reads more natural, but I also feel that the mock behavior should not be part of the action on the SUT.
The example is made up, but the key element here is the action towards the engine that is taken as a consequence of the test action given in the When. I.e. the engine response cannot be phrased as a state (unless one would say "given a malfunctioning engine".)