简体   繁体   English

测试 wai 应用程序的 state 更新

[英]Testing the state updates of a wai application

I have an application written on top of Wai , configured to have some custom state and be testable with Test.Hspec.Wai .我有一个在Wai之上编写的应用程序,配置为具有一些自定义 state 并且可以使用Test.Hspec.Wai进行测试。

I can test request/response interactions, but I haven't been able to figure out how to test for state changes;我可以测试请求/响应交互,但我无法弄清楚如何测试 state 更改; specifically, if my application state is a TVar Text , how do I get the value out of it inside the test, in order to verify its value?具体来说,如果我的应用程序 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"

*) Note that the getState method I'm talking about here is not exported in the latest LTS at the time of writing; *) 请注意,我这里所说的getState方法在撰写本文时并未在最新的 LTS 中导出; add hspec-wai-0.10.1 to extra-deps to get a version that has all the features mentioned here.将 hspec-wai-0.10.1 添加到extra-deps以获得具有此处提到的所有功能的版本。

I think the problem is that you forgot to wrap whole your spec into with app , like it is done in hspec-wai s README:我认为问题在于您忘记将整个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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM