有没有办法将命令行参数传递给spec/rspec?

问题描述 投票:0回答:2

尝试将一些参数传递给

rspec
,但找不到任何合适的命令行选项。我可以为此使用环境变量,但不确定这是配置测试的最佳(也是最透明)方法。

那么,如何传递命令行参数呢?如果不能 - 哪个是最好且最可接受的替代方案?

ruby rspec automated-tests
2个回答
2
投票

一种简洁的方法是为您的选项添加标签,尽管这可能不适用于您的情况。

rspec --tag sleep_5_seconds

然后在你的spec_helper.rb中

RSpec.configure do |c|
  before(:each, :sleep_5_seconds => true) do
    setup_5_second_sleep
  end
end

这段代码还没有运行过,但这就是概念。如果您需要根据变量进行设置和拆卸,您也可以使用 around 块来完成。


0
投票

我为此使用了双破折号。下面的示例代码可能不太惯用(我是 Ruby 新手),但对于我传递目录以供规范验证的用例来说,效果足够好。

dirs = ARGV.dup
dirs.shift until dirs.first == "--"
dirs.shift
dirs.each do |dir|
  describe "#{dir} does the thing I want..." do
    # verify dir here
  end
end

调用为

bundle exec rspec . -- [dir1] [dir2...]

© www.soinside.com 2019 - 2024. All rights reserved.