我有一个类使用the aws-sdk-rails gem(aws-sdk-ruby v2上的包装器)读取/处理来自SQS队列的消息。如何模拟AWS调用以便我可以测试我的代码而无需访问外部服务?
communicator.rb :
define( 'DB_NAME', ..... (redacted code for security)
...........................
define('AUTH_KEY'......
..........................
答案 0 :(得分:6)
我很难找到模拟AWS资源的示例。我花了几天时间搞清楚,并希望在Stack Overflow上分享我的结果以供后代使用。我使用了rspec-mocks(doubles& verifying doubles)。这是问题中include(dbsalts.php);
示例的示例。
<强> communicator_spec.rb 强>:
class Communicator
def consume_messages
sqs_client = Aws::SQS::Client.new
# consume messages until the queue is empty
loop do
r = sqs_client.receive_message({
queue_url: "https://sqs.region.amazonaws.com/xxxxxxxxxxxx/foo",
visibility_timeout: 1,
max_number_of_messages: 1
})
break if (response.message.length == 0)
# process r.messages.first.body
r = sqs_client.delete_message({
queue_url: "https://sqs.region.amazonaws.com/xxxxxxxxxxxx/foo",
receipt_handle: r.messages.first.receipt_handle
})
end
end
end
答案 1 :(得分:5)
AWS SDK已提供存根。剂适量http://docs.aws.amazon.com/sdkforruby/api/Aws/ClientStubs.html了解更多信息(与官方文档相关联。)