[英]Testing the state updates of a wai application
我有一個在Wai之上編寫的應用程序,配置為具有一些自定義 state 並且可以使用Test.Hspec.Wai進行測試。
我可以測試請求/響應交互,但我無法弄清楚如何測試 state 更改; 具體來說,如果我的應用程序 state 是TVar Text
,我如何在測試中從中獲取值,以驗證其值?
-- app :: TVar Text -> IO Application
-- is defined in my application library
wrapp :: Text -> IO (TVar Text, Application)
wrapp s = do
s' <- newTVarIO s
a <- app s'
return (s', a)
spec :: Spec
spec = withState (wrapp "hello") $ do
describe "x" $ it "x" $ do
st <- getState -- st is TVar Text.
s <- undefined -- WHAT DO I PUT HERE TO GET THE STATE OUT OF TVar?
get "/" `shouldRespondWith` "\"hello, world!\""
s `shouldBe` "hello"
*) 請注意,我這里所說的getState
方法在撰寫本文時並未在最新的 LTS 中導出; 將 hspec-wai-0.10.1 添加到extra-deps
以獲得具有此處提到的所有功能的版本。
我認為問題在於您忘記將整個spec
包裝到with app
中,就像在hspec-wai
的自述文件中所做的那樣:
spec :: Spec
spec = with app $ do
describe "GET /" $ do
it "responds with 200" $ do
get "/" `shouldRespondWith` 200
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.