8000 Messaging pacts - guidelines to provide real message to verifier · Issue #538 · pact-foundation/pact-net · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Messaging pacts - guidelines to provide real message to verifier #538

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
scrocquesel-ml150 opened this issue Apr 22, 2025 · 3 comments
Closed

Messaging pacts - guidelines to provide real message to verifier #538

scrocquesel-ml150 opened this issue Apr 22, 2025 · 3 comments 8000

Comments

@scrocquesel-ml150
Copy link

I'm working on implementing the messaging verifier.
In my case, the provider sends messages to an Azure Service Bus in response to certain interactions. I want to verify that these messages conform to the consumer pact.

Looking at the examples, it seems that messages are manually constructed in the tests. #459 call them canned message. IMHO, this introduces a risk of divergence between the actual messages sent by the application and the ones defined in the test.

What I’ve done instead is configure the provider to send messages to a Service Bus Testcontainer during integration tests. A MessageProcessor dequeues the messages and transforms them into Pact scenarios. Once all integration tests have passed, I use the collected scenarios with the message pact verifier.

What do you think of this approach?

@adamrodger
Copy link
Contributor

Would you be able to expand on your concern that the test messages may differ from the "real" ones?

I'd expect that they were exactly the same, as in if the real message creates an OrderCreatedMessage then I'd expect the test scenario setup to do something like:

var message = new OrderCreatedMessage(42, "foo");
return message; 

i.e. I'd expect the real message and the test message to both be instances of exactly the same class/record, so there's no way they can diverge.

If you're concerned about some fields not being set or something then you could always use an auto generation framework like AutoFixture, although I prefer not to do that otherwise the pact changes every run and needs to be verified again. If it stays the same (with fixed values) then verification can be implicit instead of needing to run again.

@scrocquesel-ml150
Copy link
Author

The thing is "you expect that they were exactly the same.". PactNet is all about not expecting but asserting.

In the sample, the message is published for real in

await this.publisher.PublishAsync(new OrderCreatedEvent(order.Id));

And then, manually craft,

this.verifier
.WithHttpEndpoint(ProviderUri)
.WithMessages(scenarios =>
{
scenarios.Add("an event indicating that an order has been created", () => new OrderCreatedEvent(1));
}, Options)
.WithFileSource(new FileInfo(pactPath))
.WithProviderStateUrl(new Uri(ProviderUri, "/provider-states"))
.Verify();

How do you ensure that the message created in OrderRepository within this scenario is exactly the same as the one used in the test? I have a dozen scenarios where the content varies because each input scenario is different.

For example, in the sample, an order might be created in a "Pending" state due to a payment failure, and this specific scenario would be relevant to the payment domain.

Maintaining both integration tests and canned message tests would be burdensome.

What I would like to do is running the application like its done with http interaction. Have in some way a listener to the message that will be published and populate scenarios to be verified. Then, for each scenario, I would trigger what will produce the message.

@adamrodger
Copy link
Contributor

Ah, I see the issue now. Pact is not an end-to-end testing framework like Cypress or Playwright, and as such it's not supposed to be used for testing behaviour. You're only supposed to test the "shape" of the data, not that the contents make semantic sense.

In your scenario above, you would have a matcher which ensures that the order status can be one of a number of defined strings, perhaps something like Match.Regex("^(Open|Pending|Closed)$"), and then you are sure that the status will never be anything but that. Or at least, you're sure that if the status does have a different value then your pact verification will fail and then you can investigate and fix at CI time rather than post-deployment.

You can find more info in the docs at https://docs.pact.io and particularly this section: https://docs.pact.io/consumer/contract_tests_not_functional_tests

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants
0