问题描述
有没有一种方法可以在周围添加if语句:
invalid_customer = RuntimeError.new("error code")
StripeMock.prepare_error(invalid_customer,:create_source)
基于是否不调用live_only或live标签?
我将努力消除实时测试和模拟测试,并且只有一个,因为它是多余的。
结果
rspec spec --tag live_only => would not run the above StripeMock code
rspec spec => would run the mock code
Rspec
it "should raise error when invalid customer (Live Version)",live_only: true do
err = "error code"
expect {
payment_gateway.add_source(customer_id: "invalid-id",source_id_or_token: default_card_token)
}.to raise_error(RuntimeError) {|e|
expect(e.message).to eql err
}
end
it "should raise error when invalid customer (Mock Version)",live: false do
invalid_customer = RuntimeError.new("error code")
StripeMock.prepare_error(invalid_customer,:create_source)
expect {
payment_gateway.add_source(customer_id: "invalid-id",source_id_or_token: default_card_token)
}.to raise_error(RuntimeError) {|e|
expect(e.message).to eql "error code"
}
end
已更新
我找到了一种设置变量@running_mock的方法,请参见下面的答案。
解决方法
标记以documented in rspec的形式存储在rspec metadata
中。
如果您确实需要,可以访问它们并在其上分支:
example.metadata[:live_only]
,
我找到了一种设置变量@running_mock的方法。
config.before(:example) do
@running_mock ||= (config.filter_manager.inclusions.rules.to_hash.keys & [:live,:live_only]).blank?
end
RSpec
it "should raise error when invalid customer (Live & Mock Version)",live: true do
if @running_mock
invalid_customer = RuntimeError.new("error code")
StripeMock.prepare_error(invalid_customer,:create_source)
end
expect {
payment_gateway.add_source(customer_id: "invalid-id",source_id_or_token: default_card_token)
}.to raise_error(RuntimeError) {|e|
expect(e.message).to eql "error code"
}
end