javascript 如何使用 jasmine.js 测试控制台输出?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19825020/
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-27 16:56:49  来源:igfitidea点击:

How can I use jasmine.js to test for console output?

javascriptconsolejasmine

提问by Joe Susnick

I'm working through the text: Professional JavaScript for Web Developers by Nicholas Zakasand I'm testing the examples with Jasmine.js.

我正在阅读文本:Nicholas Zakas 所著的 Web 开发人员的专业 JavaScript,我正在使用 Jasmine.js 测试这些示例。

I can currently test the output of a function by specifying a return a value, but I'm running into trouble when there are multiple pieces of data that I want to return.

我目前可以通过指定返回值来测试函数的输出,但是当我想要返回多条数据时,我遇到了麻烦。

The textbook uses the alert() method, but this is cumbersome and I don't know how to test for alerts. I was wondering if there was a way to test for console.log() output. For instance:

教科书使用了alert()方法,但是这个很麻烦,不知道如何测试alert。我想知道是否有办法测试 console.log() 输出。例如:

function_to_test = function(){
    var person = new Object();
    person.name = "Nicholas";
    person.age = 29;

    return(person.name);    //Nicholas
    return(person.age);     //29
});

I know I can have them return as one string, but for more complicated examples I'd like to be able to test the following:

我知道我可以让它们作为一个字符串返回,但是对于更复杂的示例,我希望能够测试以下内容:

function_to_test = function(){
    var person = new Object();
    person.name = "Nicholas";
    person.age = 29;

    console.log(person.name);    //Nicholas
    console.log(person.age);     //29
});

The Jasmine test looks something like:

Jasmine 测试如下所示:

it("should test for the function_to_test's console output", function(){
    expect(function_to_test()).toEqual("console_output_Im_testing_for");
});

Is there a simple way to do this that I'm just missing? I'm pretty new to coding so any guidance would be appreciated.

有没有一种简单的方法可以做到这一点,我只是想念?我对编码很陌生,所以任何指导都将不胜感激。

回答by Jason

There are a couple of things that are wrong with your code above. As pointed out, having multiple return statement will not work. Also, when defining a new object, it is preferable to use the object literal syntax over using new Object.

上面的代码有一些错误。正如所指出的,有多个 return 语句是行不通的。此外,在定义新对象时,最好使用对象字面量语法而不是使用new Object.

What you would normally do is something more like this:

你通常会做的是更像这样的事情:

var function_to_test = function () {
  var person = {
    name : "Nicholas",
    age : 29
  };

  return person;
};

Using the function above, there are a couple of ways you could test this. One it to mock out the console.log object using Jasmine Spies.

使用上面的函数,有几种方法可以测试它。一种是使用Jasmine Spies模拟 console.log 对象。

it("should test for the function_to_test's console output", function () {
    console.log = jasmine.createSpy("log");
    var person = function_to_test();
    expect(console.log).toHaveBeenCalledWith(person);
});

The other is to test the actual output of the function.

另一种是测试函数的实际输出。

it("should return a person", function () {
    var person = function_to_test();
    expect(person).toEqual({ name : "Nicholas", age : 29 });
});