在RESTBucks示例中, 作者提供了一个有用的状态图,该图描述了客户端可以针对该服务执行的操作。
这样的应用程序状态图从何而来? 好吧,这当然是从需求中得出的。
由于我喜欢使用示例指定需求 ,因此让我们看看如何从BDD风格的需求中得出应用程序状态图。
示例:RESTBucks状态图
以下是订购饮料故事的三种情况:
Scenario: Order a drink
Given the RESTBucks service
When I create an order for a large, semi milk latte for takeaway
Then the order is created
When I pay the order using credit card xxx1234
Then I receive a receipt
And the order is paid
When I wait until the order is ready
And I take the order
Then the order is completed
Scenario: Change an order
Given the RESTBucks service
When I create an order for a large, semi milk latte for takeaway
Then the order is created
And the size is large
When I change the order to a small size
Then the order is created
And the size is small
Scenario: Cancel an order
Given the RESTBucks service
When I create an order for a large, semi milk latte for takeaway
Then the order is created
When I cancel the order
Then the order is canceled
让我们从快乐路径场景开始更详细地研究这一点。
Given the RESTBucks service
When I create an order for a large, semi milk latte for takeaway
第一行告诉我在给定的广告牌URL处有一个REST服务。 第二行告诉我可以在该URI上使用POST
方法来创建具有给定属性的Order资源。
Then the order is created
这告诉我POST
返回201以及创建的Order资源的位置。
When I pay the order using credit card xxx1234
Then I receive a receipt
And the order is paid
这告诉我,从收据资源到订购资源之间存在链接。 它还告诉我订单现在处于已paid
状态。
When I wait until the order is ready
这告诉我可以使用GET
刷新Order,直到其他进程将其状态更改为ready
为止。
And I take the order
Then the order is completed
以相似的方式分析其他两种情况,我们得到的状态图与RESTBucks示例中的原始状态图非常相似。
唯一的不同是,此图在此处包含从收据导航至订单的其他操作。 本书中也描述了这种导航方式,但未在本书的图表中显示。
使用BDD技术开发RESTful API
使用BDD场景,很容易发现应用程序状态图。 这不足为奇,因为BDD场景的Given/When/Then
语法只是描述状态和状态转换的另一种方式。
从应用程序状态图中,这只是完成完整资源模型的一小步。 实施资源模型后,您可以重新使用BDD方案以自动验证实施是否符合要求。
总之,在开发RESTful API时,BDD技术可以为我们提供很多帮助。
翻译自: https://www.javacodegeeks.com/2014/07/behavior-driven-restful-apis.html