简体   繁体   English

测试快照Web框架处理程序

[英]Test Snap Web Framework Handler

I want to write little integration tests for my Snap web handlers but I am stuck. 我想为Snap Web处理程序编写一些集成测试,但遇到困难。 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: 我有一个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)

The Snap.Test module has some things to help build up a request and I use it to make a request for my handler: Snap.Test模块具有一些帮助建立请求的功能,我使用它为我的处理程序发出请求:

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. 这是问题所在,我想为此处理程序创建一个TestUnit TestCase,因此我需要在createOwnerReq请求上运行该处理程序。 The module Snap.Test provides: Snap.Test模块提供:

 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 . 我的请求处理程序是AppHandler ()类型,但runHandler需要一个Snap a类型的处理程序。 How do I lift my AppHandler type into the Snap monad? 如何将我的AppHandler类型提升到Snap monad中? Help please, this is kind of trippin' me out. 请帮助,这是我的绊脚石。

Ibolla's return create trick probably doesn't do what you want. Ibolla的return create技巧可能无法满足您的要求。 It compiles correctly because runHandler takes a Snap a which will work on a Snap action with any return value. 它可以正确编译,因为runHandler会使用Snap a ,它将对具有任何返回值的Snap操作起作用。 return create :: Snap (AppHandler ()) , which is very different from the Snap () that you were probably expecting. return create :: Snap (AppHandler ()) ,这与您可能期望的Snap ()非常不同。

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. 我们正在开发等效的Snap.Snaplet.Test,它将包装Snap.Test提供的runHandler函数以允许您测试Handlers。 This will probably be included in the 0.10 release of the snap package. 这可能会包含在snap软件包的0.10版本中。

In the interim, you can solve the problem manually by using runSnaplet to convert your SnapletInit into a Snap () action that can be passed to Snap.Test.runHandler. 在此期间,您可以通过使用runSnapletSnapletInit转换为可传递给Snap.Test.runHandler的Snap ()操作来手动解决问题。 This won't let you test an individual Handler , but it will let you test any of the routes defined in your application's initializer. 这不会让您测试单个Handler ,但是会让您测试应用程序的初始化程序中定义的任何路由。

EDIT: In snap-0.10, we added test support for snaplets . 编辑:在snap-0.10中,我们添加了对snaplets的测试支持

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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