vendredi 15 mai 2015

Stub AWS services to return an object value in Cucumber/Capybara

In the Rails app I am building, we are using the results of AWS (DynamoDB) calls made by the controller to populate the view, as part of our replacing "standard" Rails SQL storage with DynamoDB storage. The normal requests and responses run over the wire. So this is one of those inevitable situations of Cucumber capybara stubbing where it is absolutely necessary to stub in Cucumber/Capybara and return a predetermined result (AWS DynamoDB table objects with data, or some simulation of such objects).

Unfortunately, I'm not sure how to create the stubs. In features/support/webmock.rb:

AWS.stub!

or

stub_request(:post, <dynamo_db_url_string>).with(body: table_name_json).to_return(status: 200, body: '', headers: {})

will return an empty result, which cannot be used because this section of the program directly works with the DynamoDB table and its data. In particular, stub_request restricts the types of objects returned. I would like to return AWS::DynamoDB objects, which are not compatible with the stub_request command as far as I know.

I also tried

dynamo = AWS::DynamoDB.new(stub_requests: true)
AWS::DynamoDB.stub(:new).to_return(dynamo)

in both the features/support/webmock.rb and the individual Capybara steps. However, I get this error:

The use of doubles or partial doubles from rspec-mocks outside of the 
per-test lifecycle is not supported. (RSpec::Mocks::OutsideOfExampleError)

I prefer not to use a third party library, but would if there is no other way to accomplish this kind of stubbing.

Aucun commentaire:

Enregistrer un commentaire