Any idea how to write command line application in ruby with rspec ?
您知道如何使用rspec在ruby中编写命令行应用程序吗?
How to test drive a command Application.
如何测试驱动命令应用程序。
3 个解决方案
#1
2
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:
您应该看看aruba,它允许您使用Cucumber来驱动命令行应用程序开发,实际上,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 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
测试命令行应用程序需要很多技巧,但是aruba是一个很大的帮助
#2
6
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.
然而,要能够在较低的级别上测试命令,就需要以一种易于从单个单元中提取的方式对其进行结构化。如果您的脚本是一个大型的过程性脚本,那么您可能不能做很多事情,但是如果它是由表示它将要运行的各种命令的类组成的,那么您可以在shell中不执行命令的情况下测试这些类。
#3
0
Watch Ryan Bigg's pilot screencast which gives some good tips on test driving a vanilla ruby app
看看Ryan Bigg的试播视频吧,它提供了一些测试驾驶普通ruby应用的好技巧
#1
2
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:
您应该看看aruba,它允许您使用Cucumber来驱动命令行应用程序开发,实际上,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 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
测试命令行应用程序需要很多技巧,但是aruba是一个很大的帮助
#2
6
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.
然而,要能够在较低的级别上测试命令,就需要以一种易于从单个单元中提取的方式对其进行结构化。如果您的脚本是一个大型的过程性脚本,那么您可能不能做很多事情,但是如果它是由表示它将要运行的各种命令的类组成的,那么您可以在shell中不执行命令的情况下测试这些类。
#3
0
Watch Ryan Bigg's pilot screencast which gives some good tips on test driving a vanilla ruby app
看看Ryan Bigg的试播视频吧,它提供了一些测试驾驶普通ruby应用的好技巧