Hi all, I would like to use matchers with message...
# pact-python
n
Hi all, I would like to use matchers with messages, but all the examples I see have exact values https://github.com/pact-foundation/pact-python/tree/master/examples/message . Do matchers work with messages ? If so where can I find an example / documentation ?
m
You can, the examples and docs aren't great. I was looking at adding to them but some areas are a little confusing!
I'm just looking at some tests I have in a code base, but I think copy pasting chunks might also be quite confusing. There's a PR to change the current behaviour for this I believe, but something that was missing/unclear is you need to do this:
Copy code
with pact:
        mock_slack(kwargs)

        result = consumer(matchers.get_generated_values(event)).get_result()
Where the event has your matchers in, so something like
Copy code
TERM_ANY_EPIC = Term("^(audusd|eurcad|eurgbp|eurusd|gbpcad|gbpeur|gbpusd|usdcad|usdjpy)$", "audusd")
TERM_BUY_SELL = Term("^(BUY|SELL)$", "BUY")

event = {
        "detail-type": "placeTrade",
        "source": "ig-tracker",
        "detail": {
            "message": {
                "epic": TERM_ANY_EPIC,
                "trade_type": TERM_TRADE_TYPE,
                "buysell": TERM_BUY_SELL,
            }
        },
    }
The key bit being you need to make that
matchers.get_generated_values(event)
call to handle the Term properly
🤔 hopefully that makes sense / helps a little!
n
Brilliant. Thank you very much
m
You're welcome 🙂