我使用WebTestClient的(黄瓜) BDD单元测试失败了(我认为它应该通过了)。经过一些调试后,我确定这是因为CSRF检查失败了,这意味着mutateWith(csrf())操作不工作。我做错了什么?
我的测试场景:
Scenario Outline: Login
Given that player "<player>" exists with password "<password>"
And presenting a valid CSRF token
When log in as "<player>" using password "<password>"
Then program accepts the login我的测试步骤代码(注意client.mutateWith(csrf())的存在):
@SpringBootTest(...)
@AutoConfigureWebTestClient
public class WebSteps {
@Autowired
private WebTestClient client;
...
private WebTestClient.ResponseSpec response;
@Given("presenting a valid CSRF token")
public void presenting_a_valid_CSRF_token() {
client.mutateWith(csrf());
}
@When("log in as {string} using password {string}")
public void log_in_as_using_password(final String player,
final String password) {
response = client.post().uri("/login")
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
.body(BodyInserters.fromFormData("username", player)
.with("password", password))
.exchange();
}
@Then("program accepts the login")
public void program_accepts_the_login() {
response.expectStatus().isFound().expectHeader().valueEquals("Location",
"/");
}
...发布于 2019-03-24 17:24:58
尽管它的名称,mutateWith()方法并没有真正改变它的对象。相反,它返回一个已经应用了变异的新对象。因此,而不是写作
@Given("presenting a valid CSRF token")
public void presenting_a_valid_CSRF_token() {
client.mutateWith(csrf());
}写
@Given("presenting a valid CSRF token")
public void presenting_a_valid_CSRF_token() {
client = client.mutateWith(csrf());
}这个错误更可能发生在黄瓜测试中,因为测试步骤更改共享状态( client对象)的方式,而不是使用包含长调用链的fluent API。
https://stackoverflow.com/questions/55326480
复制相似问题