I'm making a few simple functions for oauth, one of which validates that there is a scope, redirectUri, and clientId in its options hash. Additionally, if these don't exist, then it should return callback(new Error(...)):
function(opts, callback) {
var scope = opts.scope;
var clientId = opts.clientId;
var redirectUri = opts.redirectUri;
if (!scope) {
return callback(new Error('No scope provided'));
}
...
}
In my tests, I test this by asserting that if no scope is provided, that it should return something of type Error and the error should match that description:
it('should require a scope', function() {
var callback = sinon.spy();
myOauthFunction({}, callback);
callback.args[0].should.be.an.Error;
callback.args[0].toString().should.eql('Error: No scope provided');
});
However, when I run the tests, I get this error:
AssertionError: expected [ [Error: No scope provided] ] to be an error. The second assertion doesn't cause issues, meaning that the argument has a toString property, but isn't of type Error. I'm kind of confused - why isn't it able to assert it's an Error?
As you can maybe see in the output message args[0] is actually an array. args[0] refers to the arguments of the first call, args[1] to the arguments of the second call, etc.
If you want to check the first argument of the first call, you have to do
callback.args[0][0].should.be.an.Error;
or
callback.getCall(0).args[0].should.be.an.Error;
More info in the documentation.