首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WebTestClient mutateWith显然没有变异

WebTestClient mutateWith显然没有变异
EN

Stack Overflow用户
提问于 2019-03-24 17:24:58
回答 1查看 1.4K关注 0票数 1

我使用WebTestClient的(黄瓜) BDD单元测试失败了(我认为它应该通过了)。经过一些调试后,我确定这是因为CSRF检查失败了,这意味着mutateWith(csrf())操作不工作。我做错了什么?

我的测试场景:

代码语言:javascript
复制
  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())的存在):

代码语言:javascript
复制
@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",
               "/");
   }

...
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-03-24 17:24:58

尽管它的名称,mutateWith()方法并没有真正改变它的对象。相反,它返回一个已经应用了变异的新对象。因此,而不是写作

代码语言:javascript
复制
   @Given("presenting a valid CSRF token")
   public void presenting_a_valid_CSRF_token() {
      client.mutateWith(csrf());
   }

代码语言:javascript
复制
   @Given("presenting a valid CSRF token")
   public void presenting_a_valid_CSRF_token() {
      client = client.mutateWith(csrf());
   }

这个错误更可能发生在黄瓜测试中,因为测试步骤更改共享状态( client对象)的方式,而不是使用包含长调用链的fluent API。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55326480

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档