使用 RSpec 测试纯 Ruby bin/my_app.rb 应用程序?
我有一个用纯 Ruby 编写的命令行(非 RAILS)应用程序,我通过 Cucumber 和 RSpec 驱动它。它遵循 lib、bin、spec 和 feature 目录的典型应用程序层次结构。
到目前为止,我一直遵循编写失败的 Cucumber 功能/场景的传统流程,下降到 RSpec 以排除支持的 lib 文件,然后让场景通过。
不幸的是,当驱动“bin/my_application.rb”中的主应用程序入口点时,这似乎并不那么简单。对我来说,主要问题是我没有在 RSpec 中描述类,它是一个顺序 Ruby 脚本,用于通过命令行参数和选项管理应用程序的类和初始化。
“bin/my_application.rb”只是一个由 shell 执行的小型包装器,用于解析命令行选项并将它们作为初始化选项传递到我的主应用程序类上。我仍然想测试 bin 脚本的行为(例如 MyApp.should_receive(option_a).with(parameter))。
有什么建议/想法/建议吗?这是驱动命令行 Ruby 脚本行为的正常测试策略吗?
提前致谢。
I have a command line (NON-RAILS) application written in pure Ruby that I'm driving out through Cucumber and RSpec. It follows the typical application hierarchy of lib, bin, spec, and feature directories.
Up until now, I've followed the traditional process of writing a failing Cucumber feature/scenario, dropping down to RSpec to drive out the supporting lib files, then getting the scenario to pass.
Unfortunately, this doesn't seem to be as straight forward when driving out the main application entry point in "bin/my_application.rb". The main issue for me is that I'm not describing a class in RSpec, it's a sequential Ruby script for managing the application's classes and initialization via command line parameters and options.
"bin/my_application.rb" is just a small shell-executed wrapper for parsing command line options and passing them onto my main application class as initializer options. I'd still like to test the behavior of the bin script (e.g. MyApp.should_receive(option_a).with(parameter)).
Any suggestions/thoughts/advice? Is this a normal test strategy for driving out command line Ruby script behavior?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不确定我完全理解你在问什么,但我想说,如果你想使用 RSpec 来测试你的参数传递,它应该很容易做到。假设您有包装器脚本:
只需将其混合并使其成为适合测试的类即可。
现在,您唯一没有测试的是
run
命令上的默认参数以及文件my_application.rb
中的一行希望有所帮助。
布兰登
Not sure I fully comprehend what you're asking, but I'd say that if you want to use RSpec to test your parameter passing it should be easy enough to do. Say you have your wrapper script:
Just mix it up and make it a class suitable for testing.
Now the only thing you don't have tested is your default parameter on the
run
command and the one line in the filemy_application.rb
Hope that helps.
Brandon