function Promise() { [native code] }
...
userCallback(err, data);
}
tryResolveFromStored();
};
var request = {
promise: havePromises ? function() {
if (!promise) {
promise = new AWS.Promise(function (resolve_, reject_) {
resolve = resolve_;
reject = reject_;
});
}
tryResolveFromStored();
return promise;
} : undefined,
...
mock = function (service, method, replace) {
// If the service does not exist yet, we need to create and stub it.
if (!services[service]) {
services[service] = {};
/**
* Save the real constructor so we can invoke it later on.
* Uses traverse for easy access to nested services (dot-separated)
*/
services[service].Constructor = traverse(_AWS).get(service.split('.'));
services[service].methodMocks = {};
services[service].invoked = false;
mockService(service);
}
// Register the method to be mocked out.
if(!services[service].methodMocks[method]) {
services[service].methodMocks[method] = { replace: replace };
// If the constructor was already invoked, we need to mock the method here.
if(services[service].invoked) {
mockServiceMethod(service, services[service].client, method, replace);
}
}
return services[service].methodMocks[method];
}
...
### Use in your Tests
```js
var AWS = require('aws-sdk-mock');
AWS.mock('DynamoDB', 'putItem', function (params, callback){
callback(null, "successfully put item in database");
});
AWS.mock('SNS', 'publish', 'test-message');
/**
TESTS
...
restore = function (service, method) { if(!service) { restoreAllServices(); } else { if (method) { restoreMethod(service, method); } else { restoreService(service); } }; }
...
/**
* Restores a single mocked service and its corresponding methods.
*/
function restoreService(service) {
if (services[service]) {
restoreAllMethods(service);
if( services[service].stub)
services[service].stub.restore();
delete services[service];
} else {
console.log('Service ' + service + ' was never instantiated yet you try to restore it.');
}
}
/**
...
setSDK = function (path) { _AWS = require(path); }
...
Project structures that don't include the `aws-sdk` at the top level `node_modules` project folder will not be properly mocked
. An example of this would be installing the `aws-sdk` in a nested project directory. You can get around this by explicitly setting
the path to a nested `aws-sdk` module using `setSDK()`.
Example:
```js
var path = require('path');
var AWS = require('aws-sdk-mock');
AWS.setSDK(path.resolve('../../functions/foo/node_modules/aws-sdk'));
/**
TESTS
**/
```
### Setting the `aws-sdk` object explicitly
...
setSDKInstance = function (sdk) { _AWS = sdk; }
...
Example:
```js
var path = require('path');
var AWS = require('aws-sdk-mock');
var AWS_SDK = require('aws-sdk')
AWS.setSDKInstance(AWS_SDK);
```
### Configuring promises
If your environment lacks a global Promise contstructor (e.g. nodejs 0.10), you can explicitly set the promises on `aws-sdk-mock
`. Set the value of `AWS.Promise` to the constructor for your chosen promise library.
...