javascript 用 Jasmine 监视 console.error()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14519203/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 21:50:46  来源:igfitidea点击:

Spying on console.error() with Jasmine

javascriptunit-testingjasmine

提问by Christian

I'm actually new to JavaScript as well as Jasmine. So it might be something really obvious that fixes my problem but I can't see it.

我实际上是 JavaScript 和 Jasmine 的新手。所以它可能是解决我的问题的非常明显的东西,但我看不到它。

I want to check if (an already existing) JavaScript application calls console.error()while loading. I don't really see a way how to realise this with Jasmine. I've included the JavaScript file as well as the spec file in the SpecRunner.html. But I take it that I somehow need to "instantiate" the application in order to test if it throws any errors on the console, right?

我想console.error()在加载时检查(一个已经存在的)JavaScript 应用程序是否调用。我真的不知道如何通过 Jasmine 实现这一点。我已经在 .js 文件中包含了 JavaScript 文件和规范文件SpecRunner.html。但是我认为我需要以某种方式“实例化”应用程序以测试它是否在控制台上抛出任何错误,对吗?

Or should I include the SpecRunner.htmlcode only for this purpose into the HTML code of the app?

或者我应该将SpecRunner.html仅用于此目的的代码包含在应用程序的 HTML 代码中吗?

回答by Andreas K?berle

You can spy on console.errorlike this:

你可以这样窥探console.error

beforeEach(function(){
  spyOn(console, 'error');
})

it('should print error to console', function(){
  yourApp.start();
  expect(console.error).toHaveBeenCalled();
})

回答by AlexStack

You can override the standard console.error function like this:

您可以像这样覆盖标准的 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' );

This solution works with Jasmin or anything else. Just put the code above before the other codes and any call after this to console.error()will call the overridden function.

此解决方案适用于 Jasmin 或其他任何东西。只需将上面的代码放在其他代码之前,在此之后的任何调用console.error()都将调用被覆盖的函数。

回答by Pablo Estornut

use toThow and toThrowError http://jasmine.github.io/edge/introduction#section-Spies:_and.throwError

使用 toThow 和 toThrowError http://jasmine.github.io/edge/introduction#section-Spies:_and.throwError