Get property from union typed member in a test
- alex_mOP 2
I am trying to create a unit test where one of my templates has a member
detailsthat is typed astemplate Record with ... details : RecordDetails ... data RecordDetails = SaleRecord SaleRecordDetails | OfferRecord OfferRecordDetails ... data SaleRecordDetails = SaleRecordDetails with ... price : DecimalIn my test case, I want to be able to do a check like:
... pure (product.price == record.details.price)I get the error:
No instance for (DA.Internal.Record.HasField “price”…
How can I tell the scenario that the record should have
detailsasSaleRecordDetails? I am receiving myRecordobject from a choice that creates theRecordas aSaleRecord.Thanks again for the help!
- Gary_Verhaegen 3
When you define a new variant type, out of the box, the only way to distinguish between variants is pattern matching. So in your case, you may want to make up a function along the lines of:
checkPrice: int -> RecordDetails -> Bool checkPrice n r = case r of SaleRecord details -> n == details.price _ -> False - alex_m
Great, thanks!