Get property from union typed member in a test
App Development3 posts360 views5 likesLast activity Aug 2020
AL
alex_mOP
Aug 2020I am trying to create a unit test where one of my templates has a member details that is typed as
template Record
with
...
details : RecordDetails
...
data RecordDetails
= SaleRecord SaleRecordDetails
| OfferRecord OfferRecordDetails
...
data SaleRecordDetails = SaleRecordDetails
with
...
price : Decimal
In 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 details as SaleRecordDetails? I am receiving my Record object from a choice that creates the Record as a SaleRecord.
Thanks again for the help!
GA
Gary_Verhaegen
Aug 2020When 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
AL
alex_m
Aug 2020Great, thanks!