Tomas Aschan
Tomas Aschan

Reputation: 60664

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.

I can test request/response interactions, but I haven't been able to figure out how to test for state changes; 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?

-- 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; add hspec-wai-0.10.1 to extra-deps to get a version that has all the features mentioned here.

Upvotes: 0

Views: 164

Answers (1)

arrowd
arrowd

Reputation: 34411

I think the problem is that you forgot to wrap whole your spec into with app, like it is done in hspec-wais README:

spec :: Spec
spec = with app $ do
  describe "GET /" $ do
    it "responds with 200" $ do
      get "/" `shouldRespondWith` 200

Upvotes: -1

Related Questions