1

我是一个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"}不工作

4

1 回答 1

1

我不认为您可以在不查询数据库状态的情况下访问它。所以你需要添加一个查询来询问你的数据库状态,例如这样:

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"}
于 2017-02-27T23:30:23.773 回答