我根据上下文切换如何测试如下
> cat spec_helper.rb
def each_store(&block)
context 'with old storage', with_elasticsearch: false do
self.class_eval(&block)
end
context 'with Elasticsearch', with_elasticsearch: true do
def case_create
:
end
self.class_eval(&block)
end
end
> cat foo_spec.rb
describe 'foo' do
each_store do
context 'with default settings' do
it do |ex|
if ex.metadata[:with_elasticsearch]
expect(something).to match /foo/
else
expect(something).not_to match /foo/
end
end
end
end
end
但是不可能不在示例块中切换,而是描述和上下文块,例如
describe 'foo' do
context 'with default settings' do |c|
if c.metadata[:with_elasticsearch]
it 'should match'
else
it 'should not match'
end
end
end
因为我想使用不同的字符串作为it方法的第一个参数,具体取决于商店。