有没有快速检查 Happstack.State 方法的好方法?
我有一组 Happstack.State MACID 方法,我想使用 QuickCheck 对其进行测试,但我无法找出实现该目标的最优雅的方法。我遇到的问题是:
- 评估
Ev
monad 计算的唯一方法是通过query
或在
。IO
monad 中更新 - 无法创建纯粹的内存 MACID 存储; 这是设计使然。因此,在 IO monad 中运行意味着每次测试后都会有临时文件需要清理。
- 除了使用状态的
initialValue
之外,无法初始化新的 MACID 存储;它不能通过任意生成,除非我公开替换状态批发的访问方法。 - 解决上述所有问题意味着编写仅使用
MonadReader
或MonadState
功能的方法(并在Reader
或State 内运行测试 getEventClockTime
等。
而不是 Ev
这意味着放弃在方法定义中使用 getRandom
或可以看到的是:
- 在一次性的磁盘 MACID 存储中运行方法,每次测试后进行清理,并每次都从
initialValue
开始 - 编写方法以运行大部分代码 。
MonadReader
或MonadState
(更容易测试),并依赖于其周围的少量不可 QuickCheck 的粘合来调用getRandom
或getEventClockTime
如有必要,
是否有我忽略的更好的解决方案?
I have a set of Happstack.State MACID methods that I want to test using QuickCheck, but I'm having trouble figuring out the most elegant way to accomplish that. The problems I'm running into are:
- The only way to evaluate an
Ev
monad computation is in theIO
monad viaquery
orupdate
. - There's no way to create a purely in-memory MACID store; this is by design. Therefore, running things in the
IO
monad means there are temporary files to clean up after each test. - There's no way to initialize a new MACID store except with the
initialValue
for the state; it can't be generated viaArbitrary
unless I expose an access method that replaces the state wholesale. - Working around all of the above means writing methods that only use features of
MonadReader
orMonadState
(and running the test insideReader
orState
instead ofEv
. This means forgoing the use ofgetRandom
orgetEventClockTime
and the like inside the method definitions.
The only options I can see are:
- Run the methods in a throw-away on-disk MACID store, cleaning up after each test and settling for starting from
initialValue
each time. - Write the methods to have most of the code run in a
MonadReader
orMonadState
(which is more easily testable), and rely on a small amount of non-QuickCheck-able glue around it that callsgetRandom
orgetEventClockTime
as necessary.
Is there a better solution that I'm overlooking?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以查看 happstack-state 中包含的快速检查属性:
http://patch-tag.com/r/mae/happstack/snapshot/current/content/pretty/happstack-state/tests/Happstack/State/Tests
如果您只是在进行测试,并且想要一次性数据存储,那么您可以使用内存保护程序,它仅将状态、事件文件和检查点存储在 RAM 中。如果你失去了权力,那么你的一切状态都会消失。这对于测试来说很好,但对于真正的实时服务器来说就不行了。您链接到的那条消息谈论的是真实的实时服务器,而不仅仅是测试。
这对解决初始值问题没有帮助,但它确实使选项 1 更容易,因为您不必进行任何磁盘清理。
要替换初始值,您需要创建自己的方法来替换当前状态批发。
像:
或者什么的。
You might checkout out the quickcheck properties that are included with happstack-state:
http://patch-tag.com/r/mae/happstack/snapshot/current/content/pretty/happstack-state/tests/Happstack/State/Tests
If you are just doing testing, and you want a throw-away data store, then you can use the memory saver, which just stores the state, event files, and checkpoints in RAM. If you lose power, then all your state would be lost. That is fine for tests, but not for a real live server. That message you linked to was talk about real live servers, not just testing.
That won't help with the initialValue issue, but it does make option 1 easier since you don't have to do any disk cleanup.
To replace the initialValue, you would need to create your own method that replaces the current state wholesale.
something like:
or something.
如果您将函数编写为 MonadState(或用于查询的 MonadReader)上的多态函数,则使用 runState/runReader 设置测试工具会容易得多。
据我所知,happstack TH 代码生成器可以很好地处理这样的签名。
If you write your functions as polymorphic over MonadState (or MonadReader for queries) it can be a lot easier to set up a test harness with runState/runReader.
The happstack TH code generators are fine with signatures like that, from what I remember.