如何在不同的配置中运行rspec测试

sat*_*jit 2 ruby automated-tests rspec

我的问题一定已经解决但我找不到.

我在各种描述块中有不同文件中的测试/示例.这些测试可以在手机,平板电脑和台式机三种不同配置下运行.通过测试分组的最佳实践是什么,以便我可以一起运行它们或独立运行它们.

我尝试了以下shared_example方法

shared_examples 'tests' do |form_factor|
   it 'example 1' do 
   end
   it 'example 2' do 
   end
   it 'example 3' do 
   end
end
Run Code Online (Sandbox Code Playgroud)

在另一个文件中

 shared_examples 'tests' do |form_factor|
    it 'example 1' do 
    end
    it 'example 2' do 
    end
 end
Run Code Online (Sandbox Code Playgroud)

但是这种方法不适用于另一个文件中的测试.一旦我在另一个文件中添加另一个shared_examples块,我就得到了

WARNING: Shared example group 'tests' has been previously defined at:
  /1_spec.rb:3
...and you are now defining it at:
  /2_spec.rb:2
The new definition will overwrite the original one.
Run Code Online (Sandbox Code Playgroud)

我也尝试过使用环境变量,但是在命令行上传递所有这些

FORM_FACTOR=phone rspec .
FORM_FACTOR=table rspec . 
Run Code Online (Sandbox Code Playgroud)

看起来很笨拙.建议?我的目标是能够传递不同的form_factor或运行所有这些.所以,如果我做了类似的事情

rspec run_all

所有形状因素都应该通过并运行所有三个测试,如果我发送

rspec run_phone

只应该发送电话

Luk*_*Mac 6

添加在您spec/spec_helper.rb为不同的过滤器文件自定义配置:

RSpec.configure do |config|
  if config.filter[:type] == :desktop
    # configuration for desktop
  elsif config.filter[:type] == :tablet
    # configuration for tablet
  end
  ....
end
Run Code Online (Sandbox Code Playgroud)

是的,你仍然要为每种类型的运行单独的命令,但为什么不将它们连接成一个:

rspec --tag type:desktop; rspec --tag type:tablet; rspec --tag type:phone
Run Code Online (Sandbox Code Playgroud)

另一种选择是创建一个将运行每种类型的RSpec的测试Rake任务.