如何根据标签做条件rspec代码
How to do conditional rspec code based on tags
有没有办法在周围添加 if 语句:
invalid_customer = RuntimeError.new("error code")
StripeMock.prepare_error(invalid_customer, :create_source)
基于是否未调用 live_only 或实时代码?
我宁愿取消现场测试和模拟测试,只做一个,因为它是多余的。
结果
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的方法,请参阅下面的答案..
标签存储在 rspec metadata
中,如 documented in rspec。
如果您确实需要,您可以访问它们并对其进行分支:
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
有没有办法在周围添加 if 语句:
invalid_customer = RuntimeError.new("error code")
StripeMock.prepare_error(invalid_customer, :create_source)
基于是否未调用 live_only 或实时代码?
我宁愿取消现场测试和模拟测试,只做一个,因为它是多余的。
结果
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的方法,请参阅下面的答案..
标签存储在 rspec metadata
中,如 documented in rspec。
如果您确实需要,您可以访问它们并对其进行分支:
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