我是一个haskell新手,在用acid状态测试函数时遇到了问题,这就是我的数据结构
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"}不工作
发布于 2017-02-28 07:30:24
我认为如果不查询数据库状态,您就无法访问它。因此,您需要添加一个查询来询问您的数据库状态,例如:
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"}https://stackoverflow.com/questions/42401526
复制相似问题