如何在Haskell中创建间谍?在下面的示例中,我想通过从hspec-unit-test通过Ctx注入间谍功能来断言传递给 doPostReq 的参数(主体):
data Ctx = Ctx {
doPostReq :: Ctx -> String -> String -> IO (Maybe B.ByteString)
}
addToCart :: Ctx -> Int -> IO String
addToCart ctx prodNo = do
...
resp <- (doPostReq ctx) ctx "http://localhost:8080/addToCart" prodToCartReqDTO
...
doPostReqSpy :: Ctx -> String -> String -> IO (Maybe B.ByteString)
doPostReqSpy ctx endpoint body = do
...
cartSpec =
describe "cart" $ do
it "correctly calls addToCart via rest" $ do
let ctxMock = ctx { doPostReq = doPostReqSpy }
addToCart ctxMock 1234
??? `shouldReturn` ???
即使我尝试使用回调,也无法将断言返回给hspecs 它:
doPostReqSpy :: (String -> String) -> Ctx -> String -> String -> IO (Maybe B.ByteString)
doPostReqSpy cb ctx endpoint body = do
cb body
...
cartSpec =
describe "cart" $ do
it "correctly calls addToCart via rest" $ do
let bodyAssertion = (\body -> body `shouldBe` "...") --
let ctxMock = ctx { doPostReq = doPostReqSpy bodyAssertion }
我不知道如何使该断言返回“通过休息正确调用addToCart” -spec ♂️