Ich versuche, fail
Bedingung in meinem Schnipsel aufzurufen. Aber wenn ich sinon.stub().throws()
Methode verwende Es zeigt mir Fehler. Kann nicht in Code umgehen. Hier ist mein Code-Schnipsel:Wie man sinon.stub() behandelt. Throws() im Komponententest von Sinon JS
login() {
let loginData = this.loginData;
return this.authService.login(loginData).then(userData => {
let msg = `${this.niceToSeeYouAgain} ${userData.email}!`;
this.userAlertsService.showSuccessToast(msg);
this.navigationService.afterLoggedIn();
//above lines are covered in test cases
}, errorInfo => {
// below line are needed to test
this.userAlertsService.showAlertToast(errorInfo);
});
}
** Und hier ist meine Unit-Test-Schnipsel: **
it('.login() - should throw exception - in failure case', sinon.test(() => {
let errorInfo = "some error";
let stub = sinon.stub(authService, 'login').throws();
let spy1 = sinon.spy(controller.userAlertsService, 'showAlertToast');
//call function
controller.login();
// $timeout.flush();
// expect things
console.log(stub.callCount, stub.args[0]);
}));
Bitte lassen Sie mich wissen, was falsch Diese Frage
ein ähnliches Problem haben mit Würfen und Versprechungen auf einem mock.I'm die API unter der Annahme ist die gleiche . Verwenden Sie 'rejects' anstelle von' throws' aus dem Stub. –