Unity Integration Test 03

We are going to implement the requirement: “When I press the button“Create” when there is already an object in the scene, I expect that nothing is going to change in the scene.

The scenario for this test is:

  • Given there is a cube in the scene called "object for test"
  • When I press the button “Create”
  • Then nothing is going to change in the scene.

We can notice that the "Given" step could be the same of the last scenario so we could just reuse the same method. However, if we analyse the “Then” scenario, we are being asked to check that nothing in the scene is changed after the second hit of the button “Create”. It means that, somehow, we have to memorise the state of the scene just before pressing the button “Create” the second time, when the scene is in a stable state. For this reason, there is another “Given” method called ThereIsACubeInTheSceneAndStoreItForAFollowingUse(): it has the same scenario text of the method ThereIsACubeInTheScene() but there is one more CallBefore attribute:

[CallBefore(4, "StoreTheListOfCubesInTheScene")]

This new method stores an array containing the cubes objects in the scene; Its declaration is different to the other Step Methods declarations:

   [GenericBDDMethod]

   public IAssertionResult StoreTheListOfCubesInTheScene()

   {

In this case, we are using the attribute [GenericBDDMethod] instead of “Given” “When” “Then” attributes. This particular form of Step Method declaration is useful when we want a method that could be usable as a [CallBefore] method, but it is not a proper Step Method with its own scenario text description.

The "When" step is also already used in the first scenario, used again as “When” step. We are reusing it like in the first step, but we want to bring to your attention an important point. We are already using it in a [CallBefore] attribute in the “Given” step. It means that the method PressTheButtonCreate() is called twice during the execution of the whole scenario. In this case, the method is well coded, so there is nothing to be afraid. However, sometimes you could be tempted to reuse a method that modifies the state of some property in the BDD Component, like the previous method StoreTheListOfCubesInTheScene(). In this case, you have to be very careful, because the second execution of the method could have an unexpected behaviour. It is better to use always stateless step methods unless that behaviour is intentional and the scope of the method is clearly to modify the state of some component property.

 

In the "Then" method declaration we can see the new parameter "Delay":

 [Then("nothing is going to change in the scene.", Delay = 1000)]

   public IAssertionResult OnlyACubeInTheScene()

   {

[...]

 

The parameter "Delay=1000"  is used to obtain a delay of 1000 milliseconds (1 sec) before the execution of the method. In this case, we are using the delay to be sure that the software has finished all the operations for the “probable” modification of the scene. According to your software behaviour, you can surely find a better way to develop this kind of algorithm. It was just a pretext for showing you the Delay parameter.

 

Back to: Unity Integration Test 02 Read next: Unity Integration Test 04

 

© 2017 Hud Dimension. All Rights Reserved.

Search