Ответ 1
Вы можете следить за console.error
следующим образом:
beforeEach(function(){
spyOn(console, 'error');
})
it('should print error to console', function(){
yourApp.start();
expect(console.error).toHaveBeenCalled();
})
Я действительно новичок в JavaScript, а также в Jasmine. Так что это может быть что-то действительно очевидное, что исправляет мою проблему, но я не вижу ее.
Я хочу проверить, что (уже существующее) приложение JavaScript вызывает console.error()
во время загрузки. Я не вижу способа понять это с Жасмин. Я включил файл JavaScript, а также файл спецификации в SpecRunner.html
.
Но я полагаю, что мне как-то нужно "создать экземпляр" приложения, чтобы проверить, не вызывает ли это ошибки на консоли, не так ли?
Или я должен включить код SpecRunner.html
только для этой цели в HTML-код приложения?
Вы можете следить за console.error
следующим образом:
beforeEach(function(){
spyOn(console, 'error');
})
it('should print error to console', function(){
yourApp.start();
expect(console.error).toHaveBeenCalled();
})
Вы можете переопределить стандартную функцию console.error следующим образом:
//call the error function before it is overriden
console.error( 'foo' );
//override the error function (the immediate call function pattern is used for data hiding)
console.error = (function () {
//save a reference to the original error function.
var originalConsole = console.error;
//this is the function that will be used instead of the error function
function myError () {
alert( 'Error is called. ' );
//the arguments array contains the arguments that was used when console.error() was called
originalConsole.apply( this, arguments );
}
//return the function which will be assigned to console.error
return myError;
})();
//now the alert will be shown in addition to the normal functionality of the error function
console.error( 'bar' );
Это решение работает с Jasmin или чем-то еще. Просто поставьте код выше, чем другие коды, и любой вызов после этого на console.error()
вызовет переопределенную функцию.
использовать toThow и toThrowError http://jasmine.github.io/edge/introduction#section-Spies:_ and.throwError