【发布时间】:2015-12-30 04:19:28
【问题描述】:
我正在尝试为我的脚本编写一个 Rspec 测试,如果我的脚本正常失败,该测试将通过。
if options[:file] == false
abort 'Missing an argument'
end
Rspec:
it 'if the file argument is not given, it provides a helpful message' do
expect(`ruby example_file.rb`).to eq('Missing an argument')
end
我的测试继续失败并说
expected: 'Missing an argument'
got: ''
我不知道为什么它返回一个空字符串。我看了这些帖子没有运气:
- How can I validate exit value in rspec?
- How can I validate exits and aborts in RSpec?
- How to spec methods that exit or abort
如果您需要有关我的脚本的更多信息,请告诉我。谢谢你。
【问题讨论】:
-
您通常不希望通过使用反引号从命令行运行脚本来使用 RSpec 来测试脚本。当你这样做时,你失去了用你想要的控制级别测试事物所需的上下文。您将执行交给 shell(Bash、Cmd 等),然后等待它返回一个值或将一些文本输出到 stdout 或 stderr