Search code examples
rubyunit-testingaws-sdkstub

Stub Calls to `Aws.config.update()`


I am writing unit tests (using the Ruby MiniTest framework) for a class which, as part of the constructor, configures the AWS SDK global configuration with a set of credentials.

It does so by calling Aws.config.update() and passing a hash of credentials.

As part of the test I do not want this configuration change to take place. Is there any way I can achieve this without modifying the class being tested? ie Without adding in a condition to the method call driven by a method parameter.

Whilst this doesn't seem like a 'stub' scenario (I don't want a canned response in return from the call, I just don't want that call to result in a configuration change) I wondered if I could use the AWS SDK stubbing capabilities (described quite nicely here)


Solution

  • Stubbing without specifying a return value has the effect you intend, for example:

    expect(Aws.config).to receive(:update)
    

    afer this, running Aws.config.update will just return nil and not actually run anything. You could also use allow.

    You didn't specify what testing library you're using, but to be clear the example I gave is in RSpec.