【问题标题】:Is there a way to test events from the test fixture directly without using expectEvents?有没有办法直接从测试夹具测试事件而不使用expectEvents?
【发布时间】:2020-11-06 13:58:52
【问题描述】:

我正在尝试测试一个 Aggregate,并希望在夹具之外断言事件,甚至可能使用 Hamcrest 进行评估?

使用时间戳的示例

        fixture.given()
           .when(new UserCreateCommand("1","test@bob.com"))
           .expectEvents(new UserCreatedEvent("1","test@bob.com");

fixture 让我可以轻松测试相等性,例如该命令准确地产生了这个事件,如果我想说引入一个时间戳,例如创建事件的时间,这并不容易

        fixture.given()
           .when(new UserCreateCommand("1","test@bob.com"))
           .expectEvents(new UserCreatedEvent("1","test@bob.com", LocalDateTime.now());

这种期望永远不会起作用,因为 LocalDateTime.now() 永远不会精确地等于聚合中生成的时间戳。

我可以简单地将时间戳包含在命令负载中,但我更倾向于在聚合内部进行处理,以确保生成此时间戳的方式一致。

有没有办法从夹具中检索事件以独立于夹具进行断言,例如

   UserCreatedEvent uce = fixture.given()
           .when(new UserCreateCommand("1","test@bob.com"))
           .extractEvent(UserCreatedEvent.class)

这将允许我使用其他断言库,例如 hamcrest:

例如

   assertThat(uce.getCreatedAt(), is(greaterThanOrEqualto(LocalDateTime.now().minusSeconds(1);

【问题讨论】:

    标签: axon


    【解决方案1】:

    尽管是一个旧帖子,但对于那些偶然发现相同问题的人,可以使用另一种方法来使用

    fixture = new AggregateTestFixture<>(YourAggregate.class);    
    fixture.registerFieldFilter(new IgnoreField(YourEvent.class, "date"));
    

    在此示例中,YourEvent 可以是包含时间戳字段 date 的所有事件的基类的事件实现类。

    @NotNull(message = "The event date cannot be null")
    @Past(message = "The event date cannot be in the future")
    @JsonProperty(value = EVENT_DATE_TAG, required = true)
    private ZonedDateTime date;
    

    使用这种方法,所有对expectXXXX 测试方法的调用都会忽略date 字段。

    【讨论】:

      【解决方案2】:

      合理的问题@vcetinick!

      您实际上应该能够将匹配器与 Axon 的 Aggregate Test Fixtures 一起使用。 AggregateTestFixture 的结果验证部分提供了expectEventsMatching(Matcher&lt;? extends List&lt;? super EventMessage&lt;?&gt;&gt;&gt; matcher) 方法。顺便可以找到这个here的代码。

      在此 Axon 框架之上提供了一组合理的匹配器,您可以将它们用于一般的消息,分组在实用程序类 Matchers(您可以找到 here)下。

      有了这一切,你应该可以做这样的事情了:

      @Test
      void sampleTest() {
          FixtureConfiguration<SampleAggregate> fixture = 
              new AggregateTestFixture<>(SampleAggregate.class);
          fixture.givenNoPriorActivity()
                 .when(new UserCreateCommand("1","test@bob.com"))
                 .expectEventsMatching(
                         Matchers.exactSequenceOf(
                                 Matchers.messageWithPayload(
                                         Matchers.matches(payload -> {
                                             // Your UserCreatedEvent validation 
                                             //  disregarding the time stamp here
                                         })
                                 )
                         )
                 );
      }
      

      如果您愿意,您基本上可以在它们中配对任意数量的 Matchers 方法。

      希望这能回答你的问题@vcetinick!

      【讨论】:

        猜你喜欢
        • 2022-07-05
        • 2015-12-31
        • 2015-08-09
        • 2017-02-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-03-22
        • 1970-01-01
        相关资源
        最近更新 更多