我是haskell noob,在用酸态进行测试功能方面存在问题。这是我的数据架构
data UserState = UserState { name :: String }
deriving (Eq, Ord, Read, Show, Data, Typeable)
这是我要测试的功能:
setName :: String -> Update UserState String
setName n =
do c@UserState{..} <- get
let newName = n
put $ c { name = newName }
return newName
$(makeAcidic ''UserState ['setName ])
这是我的测试:
spec :: Spec
spec = do
describe "test" $
it "test" $ do
setName "Mike" `shouldBe` UserState{ name = "Mike"}
我不知道如何建模我的预期值。UserState{ name = "Mike"}
不起作用
我认为您无法访问数据库状态而无需查询它。因此,您需要添加查询以询问您的数据库状态,例如:
getUserState :: Query UserState UserState
getUserState = ask
然后可以写这样的测试:
withDatabaseConnection :: (AcidState UserState -> IO ()) -> IO ()
withDatabaseConnection =
bracket (openLocalState UserState{name = "initial name"})
closeAcidState
spec :: Spec
spec = do
around withDatabaseConnection $ do
describe "test" $
it "test" $ c -> do
_ <- update c (SetName "Mike")
userState <- query c GetUserState
userState `shouldBe` UserState{ name = "Mike"}