将 AWS SDK 函数存入 return 异常(自 SDK v. 2.1 起)
Stubbing AWS SDK functions to return exceptions (since SDK v. 2.1)
我在我的规范中对 AWS SDK 调用使用存根响应,我总是能够存根异常函数,然后根据情况测试它是否被适当捕获或引发。这将是一个一直工作到现在的 MWE:
let(:aws_creds) { ::Aws::Credentials.new('a', 'b') }
let(:ec2_dummy_client) { ::Aws::EC2::Client.new(credentials: aws_creds, stub_responses: true) }
...
describe 'delete_vpc' do
it 'raises on non-existent VPC' do
ec2_dummy_client.stub_responses(:delete_vpc, Aws::EC2::Errors::InvalidVpcIDNotFound)
expect{ec2_dummy_client.delete_vpc(vpc_id: "vpc-a08b44c5")}.to raise_exception(Aws::EC2::Errors::InvalidVpcIDNotFound)
end
end
不过,自从从 AWS SDK 2.0 升级到 2.1 后,我不断收到此(真实世界)错误:
Failure/Error: expect{ec2_dummy_client.delete_vpc(vpc_id: "vpc-a08b44c5")}.to raise_exception(Aws::EC2::Errors::InvalidVpcIDNotFound)
expected Aws::EC2::Errors::InvalidVpcIDNotFound, got #<ArgumentError: wrong number of arguments (0 for 2)> with backtrace:
# ./spec/lib/backends/ec2_backend_spec.rb:691:in `block (5 levels) in <top (required)>'
# ./spec/lib/backends/ec2_backend_spec.rb:691:in `block (4 levels) in <top (required)>'
我意识到我用异常代替 return 结构的方法一开始可能有点幼稚,但它确实有效。但是,正确的方法是什么?
发现有错误的存根函数是正确的方法,但 AWS 接口在 SDK v. 2.0 和 2.1 之间发生了变化。
一些错误(不是全部)现在是 class ServiceError
,其构造函数需要两个强制属性:context
和 message
。
对于一个简单的规范,只需初始化它们就足够了,因此问题中给出的MWE可以这样固定:
let(:aws_creds) { ::Aws::Credentials.new('a', 'b') }
let(:ec2_dummy_client) { ::Aws::EC2::Client.new(credentials: aws_creds, stub_responses: true) }
...
describe 'delete_vpc' do
it 'raises on non-existent VPC' do
ec2_dummy_client.stub_responses(:delete_vpc, Aws::EC2::Errors::InvalidVpcIDNotFound.new(Seahorse::Client::RequestContext.new,"VPC does not exist"))
expect{ec2_dummy_client.delete_vpc(vpc_id: "vpc-a08b44c5")}.to raise_exception(Aws::EC2::Errors::InvalidVpcIDNotFound)
end
end
注意 stub_responses()
调用中的 .new(...)
部分。
我在我的规范中对 AWS SDK 调用使用存根响应,我总是能够存根异常函数,然后根据情况测试它是否被适当捕获或引发。这将是一个一直工作到现在的 MWE:
let(:aws_creds) { ::Aws::Credentials.new('a', 'b') }
let(:ec2_dummy_client) { ::Aws::EC2::Client.new(credentials: aws_creds, stub_responses: true) }
...
describe 'delete_vpc' do
it 'raises on non-existent VPC' do
ec2_dummy_client.stub_responses(:delete_vpc, Aws::EC2::Errors::InvalidVpcIDNotFound)
expect{ec2_dummy_client.delete_vpc(vpc_id: "vpc-a08b44c5")}.to raise_exception(Aws::EC2::Errors::InvalidVpcIDNotFound)
end
end
不过,自从从 AWS SDK 2.0 升级到 2.1 后,我不断收到此(真实世界)错误:
Failure/Error: expect{ec2_dummy_client.delete_vpc(vpc_id: "vpc-a08b44c5")}.to raise_exception(Aws::EC2::Errors::InvalidVpcIDNotFound)
expected Aws::EC2::Errors::InvalidVpcIDNotFound, got #<ArgumentError: wrong number of arguments (0 for 2)> with backtrace:
# ./spec/lib/backends/ec2_backend_spec.rb:691:in `block (5 levels) in <top (required)>'
# ./spec/lib/backends/ec2_backend_spec.rb:691:in `block (4 levels) in <top (required)>'
我意识到我用异常代替 return 结构的方法一开始可能有点幼稚,但它确实有效。但是,正确的方法是什么?
发现有错误的存根函数是正确的方法,但 AWS 接口在 SDK v. 2.0 和 2.1 之间发生了变化。
一些错误(不是全部)现在是 class ServiceError
,其构造函数需要两个强制属性:context
和 message
。
对于一个简单的规范,只需初始化它们就足够了,因此问题中给出的MWE可以这样固定:
let(:aws_creds) { ::Aws::Credentials.new('a', 'b') }
let(:ec2_dummy_client) { ::Aws::EC2::Client.new(credentials: aws_creds, stub_responses: true) }
...
describe 'delete_vpc' do
it 'raises on non-existent VPC' do
ec2_dummy_client.stub_responses(:delete_vpc, Aws::EC2::Errors::InvalidVpcIDNotFound.new(Seahorse::Client::RequestContext.new,"VPC does not exist"))
expect{ec2_dummy_client.delete_vpc(vpc_id: "vpc-a08b44c5")}.to raise_exception(Aws::EC2::Errors::InvalidVpcIDNotFound)
end
end
注意 stub_responses()
调用中的 .new(...)
部分。