所以我猜测测试失败是因为之前的值被缓存了。这可能取决于缓存策略,并且每次测试可能使用不同的数据,因此缓存总是会丢失。
但是如果你需要使用类似的数据,你可以在每次测试之前清除缓存。例如:
给定这个流程,设置一个命名的对象存储和使用所述对象存储的缓存策略:
<os:object-store name="myObjectStore" doc:name="Object store"
doc:id="25cec35b-2680-4754-8073-633f3e60a34f" />
<ee:object-store-caching-strategy
name="Caching_Strategy" objectStore="myObjectStore" doc:name="Caching Strategy"
doc:id="fd059f4f-d3be-4971-a36c-374506e2db49" />
<flow name="httpCache">
<ee:cache cachingStrategy-ref="Caching_Strategy" doc:name="Cache" doc:id="0dcabdb4-d6fc-41d5-aebd-77d015e02dd7" >
<http:request method="GET" doc:name="Request" doc:id="366184e9-6c0b-41d4-8414-3dfdd047ecb2" url="http://google.com"/>
<logger level="ERROR" message="CACHE MISS" />
</ee:cache>
<logger level="ERROR" message="After cache: #[payload]" />
</flow>
然后是您的测试,设置一个之前的操作以清除该存储:
<munit:before-test name="clearOS" description="clears OS">
<os:clear objectStore="myObjectStore" doc:id="cd931ce7-6945-4dc9-919a-3ff9b158d746" />
</munit:before-test>
<munit:test name="new-test-suite-httpCacheTest" description="Test" doc:id="b8e299ea-d191-4c0a-ac71-27a12b28d275" >
<munit:behavior >
<munit-tools:mock-when doc:name="Mock when" doc:id="8bf1468d-c9c1-48a1-9009-bae02bf8e788" processor="http:request">
<munit-tools:with-attributes >
<munit-tools:with-attribute attributeName="doc:name" whereValue="Request" />
</munit-tools:with-attributes>
<munit-tools:then-return >
<munit-tools:payload value="#['mockPayload']"/>
</munit-tools:then-return>
</munit-tools:mock-when>
</munit:behavior>
<munit:execution >
<flow-ref doc:name="Flow-ref to httpCache" doc:id="2dd44c7b-f250-4852-9e53-f4eee3b5ad84" name="httpCache"/>
</munit:execution>
<munit:validation >
<munit-tools:assert-that doc:name="Assert that" doc:id="6521459d-04dd-4e59-8a2e-16a27f51c091" expression="#[payload]" is="#[MunitTools::notNullValue()]"/>
</munit:validation>
</munit:test>
<munit:test name="new-test-suite-httpCacheTest2" description="Test" doc:id="d6ea0d83-f943-450e-948c-26596bf41207" >
<munit:behavior >
<munit-tools:mock-when doc:name="Mock when" doc:id="8688876f-144e-4f7f-8ef0-98a8e6caeca0" processor="http:request">
<munit-tools:with-attributes >
<munit-tools:with-attribute attributeName="doc:name" whereValue="Request" />
</munit-tools:with-attributes>
<munit-tools:then-return >
<munit-tools:payload value="#['mockPayload2']"/>
</munit-tools:then-return>
</munit-tools:mock-when>
</munit:behavior>
<munit:execution >
<flow-ref doc:name="Flow-ref to httpCache" doc:id="b4a1d37a-f0e4-4fa1-9c78-084ed3f6faf8" name="httpCache"/>
</munit:execution>
<munit:validation >
<munit-tools:assert-that doc:name="Assert that" doc:id="46febad5-d557-4152-829e-db61aa6ef409" expression="#[payload]" is="#[MunitTools::notNullValue()]"/>
</munit:validation>
</munit:test>
这两个都将记录:
LoggerMessageProcessor: CACHE MISS
接着:
LoggerMessageProcessor: After cache: mockPayload
LoggerMessageProcessor: After cache: mockPayload2
如果没有 os:clear 它只会第一次错过缓存,并从缓存中打印出相同的有效负载:
LoggerMessageProcessor: CACHE MISS
LoggerMessageProcessor: After cache: mockPayload
LoggerMessageProcessor: After cache: mockPayload
或者,您可以将缓存和 http 处理器移动到它们自己的流中并模拟流,这样缓存就不会在您的测试中使用。