撰写JBehave故事 [英] writing a JBehave story
问题描述
这个问题与REST 无关,而是关于在随后的@Then
中使用在@When
中进行的调用返回的值.
This question isn't about REST, but about using the returned value from an invocation made in @When
in the subsequent @Then
.
我正在使用JBehave测试对REST api的一些调用.首先,有一个post
可以创建用户
I am looking at using JBehave to test some calls to a REST api. First there is a post
to create the user
When I create a user with name Charles Darwin
据我了解REST,这就是 Atom api 的作用, id在位置标头中返回,例如/user/22
.因此,我想断定有关响应的一些信息.
As I understand REST, and this is what the Atom api does, the id is returned in the location header, e.g. /user/22
. So then I want to assert something about the response.
Then user was created with a valid Id
我可以通过在Steps类中创建一个成员变量并在其中存储响应来做到这一点,我以前曾经使用过这种方法,但这是正确的方法吗?
I can do this by creating a member variable in the Steps class and storing the response there, and I have used this approach before, but is this the correct way?
推荐答案
是.需要存储可以在@Then方法中声明的数据.最简单的方法是拥有一个成员变量-但这意味着您的@ When/@ Then需要位于同一Steps类中.另一种方法是拥有一个所有步骤都使用的共享数据对象,然后可以在一种方法中进行设置,然后在另一种方法中进行获取.如果只需要通用的东西,可以将Map<String,? extends Object>
用作通用数据对象.然后,如果您使用多个线程运行,则将数据对象包装在ThreadLocal中.
Yes. One needs to store data that can be asserted on in your @Then methods. The simplest way to do this is to have a member variable - but that means that your @When/@Then need to be in the same Steps class. Another way to do it is to have a shared data object that all your Steps use and you can then set it in one method and get it in another. If you just want something generic, you can do a Map<String,? extends Object>
as your generic data object. And then if you run with multiple threads, then wrap the data object in a ThreadLocal.
这就是我所看到的-并且应该使用@ BeforeScenario/@ AfterScenario方法设置/清除数据对象.
That's what I've seen - and the data object should be setup/cleared with a @BeforeScenario/@AfterScenario method.
这篇关于撰写JBehave故事的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!