Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
7
rated 0 times [  11] [ 4]  / answers: 1 / hits: 35734  / 9 Years ago, thu, august 6, 2015, 12:00:00

I have a class that rejects a promise:



Sync.prototype.doCall = function(verb, method, data) {
var self = this;

self.client = P.promisifyAll(new Client());

var res = this.queue.then(function() {
return self.client.callAsync(verb, method, data)
.then(function(res) {
return;
})
.catch(function(err) {
// This is what gets called in my test
return P.reject('Boo');
});
});

this.queue = res.delay(this.options.throttle * 1000);
return res;
};

Sync.prototype.sendNote = function(data) {
var self = this;
return self.doCall('POST', '/Invoice', {
Invoice: data
}).then(function(res) {
return data;
});
};


In my test:



return expect(s.sendNote(data)).to.eventually.be.rejectedWith('Boo');


However while the test passes it throws the error to the console.



Unhandled rejection Error: Boo
...



With non promise errors I have used bind to test to prevent the error from being thrown until Chai could wrap and test:



return expect(s.sendNote.bind(s, data)).to.eventually.be.rejectedWith('Boo');


However this does not work with this and returns:



TypeError: [Function] is not a thenable.



What is the correct way to test for this?


More From » promise

 Answers
3

You're getting the error because sendNote is being rejected and you're not catching it.



Try:



var callPromise = self.doCall('POST', '/Invoice', {
Invoice: data
}).then(function(res) {
return data;
});

callPromise.catch(function(reason) {
console.info('sendNote failed with reason:', reason);
});

return callPromise;


Looks like you'll also have to move your existing catch one block out:



var res = this.queue.then(function() {
return self.client.callAsync(verb, method, data)
.then(function(res) {
return;
});
}).catch(function(err) {
// This is what gets called in my test
return P.reject('Boo');
});

[#65525] Tuesday, August 4, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
chase

Total Points: 78
Total Questions: 106
Total Answers: 93

Location: Comoros
Member since Tue, Mar 14, 2023
1 Year ago
chase questions
Thu, Mar 31, 22, 00:00, 2 Years ago
Thu, Jul 1, 21, 00:00, 3 Years ago
Sat, Dec 12, 20, 00:00, 4 Years ago
Mon, Sep 14, 20, 00:00, 4 Years ago
;