如何使用 Rspec 测试 Sinatra 中的辅助块?
我正在编写一个 sinatra 应用程序并使用 rspec 和rack/test 对其进行测试(如 sinatrarb.com 上所述)。
到目前为止,一切都很棒,直到我将一些相当程序化的代码从我的域对象移到
西纳特拉的帮手。
从那时起,我一直试图弄清楚如何单独测试这些?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我通过将帮助器方法放入其自己的模块中来单独测试我的 sinatra 帮助器。
由于我的 sinatra 应用程序比通常的 hello world 示例稍大一些,因此我需要将其分成更小的部分。通用助手的模块非常适合我的用例。
如果您编写一个快速演示,并在
helpers { ... }
块中定义辅助方法,我认为测试它是绝对必要的。无论如何,任何生产中的 sinatra 应用程序都可能需要更多的模块化。I test my sinatra helpers in isolation by putting the helper methods within its own module.
Since my sinatra application is a little bit bigger than the usual hello world example, I need to split it up into smaller parts. A module for the common helpers suits my use case well.
If you write a quick demo, and you define your helper methods within the
helpers { ... }
block, I don't think testing it is absolutely necessary. Any sinatra app in production, may require more modularity anyways.实际上你不需要这样做:
因为你只需调用
helpers
方法需要一个要混合的模块列表和一个在以下位置进行类评估的可选块: https://github.com/sinatra/sinatra/blob/ 75d74a413a36ca2b29beb3723826f48b8f227ea4/lib/sinatra/base.rb#L920-L923Actually you don't need to do:
Since you can just call
The
helpers
method takes a list of modules to mix-in and an optional block which is class-eval'd in: https://github.com/sinatra/sinatra/blob/75d74a413a36ca2b29beb3723826f48b8f227ea4/lib/sinatra/base.rb#L920-L923也许这可以帮助您http://japhr.blogspot.com /2009/03/sinatra-innards-deletgator.html
maybe this can help you some way http://japhr.blogspot.com/2009/03/sinatra-innards-deletgator.html
我也尝试过这个(需要清理一下才能重用)来将每个助手隔离在自己的环境中进行测试:
I've also tried this (which needs to be cleaned up a bit to be reusable) to isolate each helper in its own environment to be tested: