decoded
decoded returns every payload published to the destination as a value; decoded_with names the codec.
The values come in publish order, decoded as the record's type parameter T; decoded_as decodes them as another type.
decoded uses the default codec and exists when a codec feature is enabled.
decoded_with(&codec) decodes with the codec the mount site named, here a .codec(..) on the reply.
Either one panics, naming the destination and the type, when a payload does not decode.
with checks the latest payload against one value.
Example
- Macros
- Manual
#[tokio::test]
async fn every_order_got_its_receipt_in_order() -> Result<(), Box<dyn Error>> {
let tb = TestApp::start(app()).await?;
let broker = tb.broker::<MemoryBroker>();
for id in [7, 8] {
broker
.message(&Order { id, seats: 1 })
.to("orders")
.publish()
.await?;
}
broker
.message(&Order { id: 9, seats: 1 })
.to("legacy-orders")
.publish()
.await?;
let receipts = broker.published::<Receipt>("receipts").decoded();
assert_eq!(receipts, [Receipt { order_id: 7 }, Receipt { order_id: 8 }]);
let invoices = broker
.published::<Invoice>("invoices")
.decoded_with(&Versioned);
assert_eq!(invoices, [Invoice { order_id: 9 }]);
Ok(())
}
#[tokio::test]
async fn every_order_got_its_receipt_in_order() -> Result<(), Box<dyn Error>> {
let tb = TestApp::start(app()).await?;
let broker = tb.broker::<MemoryBroker>();
for id in [7, 8] {
broker
.message(&Order { id, seats: 1 })
.to("orders")
.publish()
.await?;
}
broker
.message(&Order { id: 9, seats: 1 })
.to("legacy-orders")
.publish()
.await?;
let receipts = broker.published::<Receipt>("receipts").decoded();
assert_eq!(receipts, [Receipt { order_id: 7 }, Receipt { order_id: 8 }]);
let invoices = broker
.published::<Invoice>("invoices")
.decoded_with(&Versioned);
assert_eq!(invoices, [Invoice { order_id: 9 }]);
Ok(())
}