测试 Snap Web 框架处理程序
我想为我的 Snap Web 处理程序编写一些集成测试,但我陷入困境。这是场景。我有一个 Snap Web 处理程序(普通风格)CRUD 类型,它看起来像这样:
create :: AppHandler ()
create = method POST $ do
lastName <- decodeUtf8 . fromJust <$> getParam "lastName"
firstName <- decodeUtf8 . fromJust <$> getParam "firstName"
createPerson $ Person firstName lastName
modifyResponse (setResponseCode 204)
Snap.Test
模块有一些东西可以帮助构建请求,并且我用它向我的处理程序发出请求:
createOwnerReq :: RequestBuilder IO ()
createOwnerReq = postUrlEncoded "host/person/create" $
fromList [ ("firstName", ["Greg-Shaw"])
, ("lastName", ["Snoy'Sullivan"])
]
问题是,我想为此处理程序创建一个 TestUnit TestCase,因此我需要在 createOwnerReq
请求上运行处理程序。模块 Snap.Test 提供:
runHandler :: MonadIO a => RequestBuilder m () -> Snap a -> m Response
所以
... do
resp <- runHandler createOwnerReq ???
但是等等!我的请求处理程序的类型为 AppHandler ()
,但 runHandler
需要一个 Snap a
类型的处理程序。 如何将我的 AppHandler
类型提升到 Snap
monad 中?请帮忙,这让我很困惑。
I want to write little integration tests for my Snap web handlers but I am stuck. Here is the scenario. I have a Snap web handler that (run-of-the-mill style) CRUDs up a type and it looks something like this:
create :: AppHandler ()
create = method POST $ do
lastName <- decodeUtf8 . fromJust <gt; getParam "lastName"
firstName <- decodeUtf8 . fromJust <gt; getParam "firstName"
createPerson $ Person firstName lastName
modifyResponse (setResponseCode 204)
The Snap.Test
module has some things to help build up a request and I use it to make a request for my handler:
createOwnerReq :: RequestBuilder IO ()
createOwnerReq = postUrlEncoded "host/person/create" $
fromList [ ("firstName", ["Greg-Shaw"])
, ("lastName", ["Snoy'Sullivan"])
]
Here's the problem, I want to make a TestUnit TestCase for this handler so I need the run the handler on the createOwnerReq
request. The module Snap.Test
provides:
runHandler :: MonadIO a => RequestBuilder m () -> Snap a -> m Response
so
... do
resp <- runHandler createOwnerReq ???
But wait!!! My request handler is of type AppHandler ()
but runHandler
requires a Handler of type Snap a
.
How do I lift my AppHandler
type into the Snap
monad? Help please, this is kind of trippin' me out.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Ibolla 的
return create
技巧可能无法达到您想要的效果。它可以正确编译,因为 runHandler 采用 Snap a ,它将对具有任何返回值的 Snap 操作起作用。return create::Snap (AppHandler())
,这与您可能期望的Snap()
非常不同。我们正在开发 Snap.Snaplet.Test 等效项,它将包装 Snap.Test 提供的 runHandler 函数,以允许您测试处理程序。这可能会包含在 snap 包的 0.10 版本中。
在此期间,您可以使用 runSnaplet 将
SnapletInit
转换为可传递给 Snap.Test.runHandler 的Snap ()
操作。这不会让您测试单个Handler
,但它可以让您测试应用程序初始化程序中定义的任何路由。编辑:在snap-0.10中,我们添加了 测试对 snaplet 的支持。
Ibolla's
return create
trick probably doesn't do what you want. It compiles correctly because runHandler takes aSnap a
which will work on a Snap action with any return value.return create :: Snap (AppHandler ())
, which is very different from theSnap ()
that you were probably expecting.We are working on a Snap.Snaplet.Test equivalent that will wrap the runHandler function provided by Snap.Test to allow you to test Handlers. This will probably be included in the 0.10 release of the snap package.
In the interim, you can solve the problem manually by using runSnaplet to convert your
SnapletInit
into aSnap ()
action that can be passed to Snap.Test.runHandler. This won't let you test an individualHandler
, but it will let you test any of the routes defined in your application's initializer.EDIT: In snap-0.10, we added test support for snaplets.