Assert json matches using Yesod.Test

88 Views Asked by At

I wan't to get a resource from my yesod app and assert it matches a known JSON value.

I can see that in Yesod.Test.Json there is the function assertJSON with the type

assertJSON :: (ToJSON a, FromJSON a) => (a -> (String, Bool)) -> SResponse -> Session ()

But there is no example usage and I can't work out how this is used from the type alone.

How would I be able to check that the JSON returned from my yesod app matches?

1

There are 1 best solutions below

0
On

It looks to me like you acquire an SResponse in a Session do block as per https://hackage.haskell.org/package/wai-test-0.1.1/docs/Network-Wai-Test.html (googled SResponse and checked that the module matched one imported by https://hackage.haskell.org/package/yesod-test-json-0.2.0.0/docs/src/Yesod-Test-Json.html), choose a type a that you want to assert the JSON to encode, construct a function a -> (String, Bool) whose second argument specifies whether your desired property holds and whose first argument specifies the error to output otherwise (should have been a -> Either String Bool imo) (checked the source), then apply assertJSON to the function and the SResponse within that same do block to express the assertion.