Use ezSpec to implement behavior-driven development and instantiate requirements (3): Writing Scenario and passing simple parameters

March 18 20:27~22:12; March 20 00:00~00:30

截圖 2023-03-20 上午12.20.34

foreword

After introducing the domain model of ezSpec (please refer to < Using ezSpec to Implement Behavior-Driven Development and Instantiation Requirements (1): Domain Model Introduction> ) and Feature and Story (please refer to < Using ezSpec to Implement Behavior-Driven Development and Instantiation Requirements (2) : Feature and Story >), finally getting into the topic, today I will introduce how to use ezSpec to write Scenario.

***

Write a Scenario

Suppose you want to develop an invoicing program that can calculate the total price including tax and tax from the untaxed amount. The first Scenario you write for this program is shown in Figure 1: “When the untaxed amount of a computer It is 20 million, and the sales tax is 5%. When you buy this computer, you should pay a total price of 21,000 including tax, of which 1,000 is sales tax.”

The way to generate a Scenario is very simple. Use feature.withStory() to find a pre-built Story (please refer to < Using ezSpec to Implement Behavior-Driven Development and Instantiation Requirements (2): Feature and Story >), and then call newScenario on the Story A new Scenario can be generated. When generating a Scenario, you can specify the name of the Scenario. If not specified, ezSpec will automatically capture the name of the test method as the name of the Scenario.

After the Scenario is generated, the actual content of the Scenario can be written through its Given, When, Then, And, But (collectively referred to as Step) and other methods. Each Step accepts two parameters:

  • String : A text description used to describe the content of the Step.
  • Lambda : The program that actually executes Step, ezSpec uses Lambda to replace Cucumber’s Step Definition.

Since the main purpose of this series of articles is to introduce how to use ezSpec, not to introduce software development through TDD/BDD/SBE, so next Teddy will explain how to specify parameters, read parameters, and pass parameters when writing a scenario Way.

截圖 2023-03-18 下午9.33.45

▲Figure 1: Scenario example of ezSpec

***

specifying and reading unnamed parameters

In the example in Figure 1, there are four parameters:

  • The untaxed amount on line 61 of 20,000
  • 5% sales tax on line 64
  • Total price including tax on line 69 21,000
  • Sales tax on line 72 1,000

There are two ways to read these parameters in the Lambda of the Step. The first method is to add the $ symbol in front of the parameters. The Lambda of the Step can pass in a ScenarioEnvironment object as a parameter, which can be read in the Lambda through the ScenarioEnvironment For the string starting with $, please refer to the env parameter in Figure 2.

截圖 2023-03-18 下午9.39.50

▲Figure 2: Scenario example of ezSpec specifying and reading unnamed parameters

Figure 2 demonstrates three functions for reading parameters:

  • Line 62 evn.getArg : returns the parameter of type String
  • Line 65 evn.getArgd : returns the parameter of type double
  • Line 70 evn.getArgi : returns the parameter of type int

The parameter specified by the $ method has only value and no key, so the data needs to be read by the index method when reading. In Figure 2, each Step happens to have only one parameter, so these parameters can be obtained through env.getArg(0).

***

specifying and reading named parameters

Since there are unnamed parameters, there are named parameters, please refer to Figure 3:

  • Specify a well-known parameter: Refer to line 61 of Figure 3 and use ${tax_included=20,000} , or use 64th ${vat_rate:5%} to specify the name of the parameter (use = or :).
  • Read famous parameters: refer to Figure 3 No. 62, 65, 70, 73, the reading method is similar to Figure 2, but at this time use the key of the string to read the parameter content.

截圖 2023-03-18 下午9.51.54

▲Figure 3: Scenario example of ezSpec specifying and reading famous parameters

***

Read parameters of other Steps

Sometimes you want to read the famous parameters defined by other Steps in Lambda. At this time, you need to use the getHistoricalArg function to obtain them. Please refer to line 74 in Figure 4: env.getHistoricalArg(“tax_included”) read line 61 Defined tax_included parameter.

截圖 2023-03-19 下午11.59.36

▲Figure 4: Example of ezSpec reading the famous parameters of other Steps

***

Transfer data in different Steps

When writing a Scenario, it is often necessary to transfer data between different Steps. For example, in the When on line 67, you use the hasBought variable to represent the successful purchase of a computer, and you want to verify the content of hasBought in the Lambda of Then. Please refer to Figure 5. At this time, you can use Put(key, value) of ScenarioEnvironment to put the data to be transferred into ScenarioEnvironment (line 69), and then use get(key, Class<T>) in another Lambda to read the data out (line 73).

截圖 2023-03-20 上午12.09.10

▲Figure 5: ezSpec transfers data in different Steps

***

epilogue

Today I will introduce how ezSpec writes Scenarios and specifies and reads parameters. Scenarios can also accept a Table data as parameters. Teddy will introduce this function in the next episode.

***

Youzo’s inner monologue: Write Step Definition with Lambda, so you don’t have to deal with annoying regular expressions.

This article is transferred from https://teddy-chen-tw.blogspot.com/2023/03/ezspec3scenario.html
This site is only for collection, and the copyright belongs to the original author.