8. Mocking Data¶
Writing a contract enables you to create a mock server or a client by just
Arbitrary instances for datatypes used in the contract.
Lets create a mock server for the contract mentioned in Quick start by writing arbitrary instances for our datatypes.
instance Arbitrary UserData where arbitrary = UserData <$> arbitrary <*> arbitrary <*> arbitrary instance Arbitrary UserToken where arbitrary = UserToken <$> arbitrary <*> arbitrary instance Arbitrary Text where arbitrary = elements ["Foo", "Bar", "Baz"]
Now we can create a
Wai.Application for our mock server as
mockApp :: Wai.Application mockApp = mockServer serverSettings (MockServer mockServerSettings :: MockServer MyApiService)
MockServer as arguments.
MockServer lets you decide what kind of mock data is to be returned
OtherError). It returns
SuccessData) by default.
Now you can run this
Wai.Application on some port to bring up your mock
main :: IO () main = run 8000 mockApp
You can even mock the requests. To create a mock
User declared in Quick start, we can write:
req <- mockClient (Res :: Resource GET User)
We can use this
req while calling
client function to make a