The breaking news is the introduction of multiple roles for a scenario, prior to this release SpecSalad could only define a single role for a scenario, with this new release multiple roles can be defined at the start of a scenario, allowing the scenario text to more closely match the business scenarios where more than a single person or process is involved.
Data and complete objects can be passed between these roles using the currently available save and restore functionality available from the application role base class.
So how does this work?
Taking the example from the Codebreaker game in a previous post, we can change the codebreaker submits guess scenario to make the intent clearer, currently the text had to say that the codebreaker set the secret code.
When in fact is should state that another component sets the code, which the codebreaker is then trying to guess.
Using the new multiple roles feature the text can now make this separation clear, specifying who is responsible for setting the secret code.
This rewritten scenario now explicitly states how the secret code is set, and that this responsibility is separate from the codebreaker role.
the new role class is created in exactly the same way as before stored within the roles directory and inheriting from the ApplicationRole
The code here is almost identical to the original code in the codebreaker role, the important change from the original is the last line this stores the code generator mock object so that it can be accessed by the codebreaker role when required.
Several changes have been made to the codebreaker role, after the removal of the secret code generator calls.
The submit guess method was changed so that it calls the start game before calling the games guess method
The “start the game” method was tidied up, removing the creation of the secret code generator mock, moving the call to a property that will return the required secret code generator..
The new secret code generator property attempts to retrieve the generator using the application role base class call to retrieve, which returns the generator defined in the secret code generator role, a check is made to ensure we have retrieved a generator before returning it.
The check for a retrieved value is required because the start game scenarios also call the “start the game” method and because these scenarios have no interest in the secret code they do not define a generator, so to ensure that these scenarios pass a default generator is defined that simply returns zero.
The SpecSalad syntax has been updated allowing scenario step to define a role, and state which role is to carry out the scenario step task.
This new syntax is compatible with scenarios written with a previous version of the framework.