如何使用 RSpec 在 Ruby 中编写命令行应用程序?

发布于 2024-12-17 07:59:06 字数 63 浏览 1 评论 0原文

知道如何使用 rspec 在 ruby​​ 中编写命令行应用程序吗?

如何测试驱动命令应用程序。

Any idea how to write command line application in ruby with rspec ?

How to test drive a command Application.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

念三年u 2024-12-24 07:59:06

如果你想运行一个命令,你可以在命令周围使用反引号并返回一个字符串:

`ruby /path/to/my_command.rb arg1 arg2`.should == "something"

但我个人会将这种测试风格限制为测试的非常有限的子集。这些是集成测试,而不是单元测试,这并不意味着您不应该编写它们,但您可能应该首先在稍低的级别进行测试。

然而,要能够在较低级别测试您的命令,需要以易于从中提取各个单元的方式构建它。如果您的脚本是一个大型过程脚本,那么您可能不能做太多事情,但如果它由代表它将运行的各种命令的类组成,您可以测试这些类,而无需在 shell 中执行命令。

If you want to run a command you can use backticks around the command and have a String returned:

`ruby /path/to/my_command.rb arg1 arg2`.should == "something"

But I personally would limit this style of testing to a very limited subset of your tests. These are integration tests, more than unit tests, which doesn't mean you shouldn't write them, but you should probably test at a slightly lower level first.

Being able to test your command at a lower level, however, requires structuring it in a way that is easy to pull individual units from. If your script is a big procedural script, then you probably can't do much, but if it's composed of Classes that represent the various commands it will run, you can test those classes without executing the command in the shell.

街道布景 2024-12-24 07:59:06

您应该查看 aruba,它允许您使用 Cucumber 驱动命令行应用程序开发,该应用程序在覆盖,使用 RSpec。它处理执行程序并捕获其输出和退出状态,这对于您自己来说可能有点痛苦。举个例子:

Given the file "foo.txt" exists
When I successfully execute `cp foo.txt bar.txt`
Then a file named "bar.txt" should exist
And the file "bar.txt" should the same contents as "foo.txt"

Aruba 为您提供第二步和第三步,您提供其他步骤:

CONTENTS = 'some contents'

Given /^the file "([^"]*)" exists$/ do |file|
  File.open(file,'w') { |file| file.puts(CONTENTS) }
end
Then /^the file "([^"]*)" should the same contents as "([^"]*)"$/ do |dest_file,source_file|
  contents = File.read(dest_file)
  contents.should == CONTENTS
end

测试命令行应用程序有很多更棘手的东西,但 aruba 提供了很大的帮助

You should look at aruba, which allows you to drive command-line app development using Cucumber, which, under the covers, uses RSpec. It handles executing programs and capturing their output and exit status, which can be kindof a pain to do on your own. As an example:

Given the file "foo.txt" exists
When I successfully execute `cp foo.txt bar.txt`
Then a file named "bar.txt" should exist
And the file "bar.txt" should the same contents as "foo.txt"

Aruba provides you the second and third steps, you provide the others:

CONTENTS = 'some contents'

Given /^the file "([^"]*)" exists$/ do |file|
  File.open(file,'w') { |file| file.puts(CONTENTS) }
end
Then /^the file "([^"]*)" should the same contents as "([^"]*)"$/ do |dest_file,source_file|
  contents = File.read(dest_file)
  contents.should == CONTENTS
end

There's a lot more tricky stuff to testing command-line apps, but aruba is a big help

呆头 2024-12-24 07:59:06

观看 Ryan Bigg 的试点截屏视频,其中提供了一些关于测试驱动普通 ruby​​ 应用程序的好技巧

Watch Ryan Bigg's pilot screencast which gives some good tips on test driving a vanilla ruby app

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文