haskellhspec

Match "any string" in Haskell record in HSpec test


I have a data class Entity which is defined like this:

data Entity = Entity { id :: String, name :: String }

and a function that returns IO Entity:

newPersistentEntity :: String -> IO Entity

And I'd like to write an HSpec test for this:

spec :: Spec
spec = describe "newPersistentEntity function" $
       it "returns a new Entity with a generated id and the specified name" $
       newPersistentEntity "myName" `shouldReturn` Entity {id = <any string>, name = "myName"}

The problem is that the id is a UUID generated by the database. I want to assert that the id is a string to make the test pass.

How can I achieve this?


Solution

  • Can you create the record, then use its id value to create the record you're comparing against? Something like:

    new <- newPersistentEntity "myName"
    new `shouldBe` Entity { id = (id new), name = "myName" }
    

    (Note: don't have enough information to test this code, treat this as somewhat pseudo code).

    As a side note, your code doesn't look like normal Persistent code, so I'm assuming you're using a different library.