【问题标题】:How to see output of `console.log` when running Jasmine tests运行 Jasmine 测试时如何查看“console.log”的输出
【发布时间】:2019-06-16 05:17:13
【问题描述】:

我开发了一个 Node.js 模块,我正在使用 Jasmine 为其编写单元测试。该模块使用console.log 打印出一些信息,因为它在调用时将verbose 参数设置为true。

假设模块如下所示:

foo.js

function foo(arg1, verbose){
    var output = "Hello " + arg1;
    if(verbose)console.log("Returning %s", output);
    return output;
}
module.exports = foo;

假设我的测试如下所示:

foo-spec.js

const foo = require("path/to/foo")
describe("Foo suite", () => {

    it( "Expect foo to greet", () => {
        expect(foo("World", true)).toBe("Hello World");
    });
});

我通过在终端中输入 jasmine 来运行我的测试:

$ jasmine

除了我想看到详细的输出之外,一切都很好。

$ jasmine
Returning Hello World

有没有办法让茉莉这样做?

【问题讨论】:

  • 你真的需要看到这个吗?在可测试性方面监视 console.log 调用不是更好吗?

标签: node.js jasmine console.log


【解决方案1】:

好的,我已经找到了一种解决方法。监视console.log 以某种方式修补了它。

foo-spec.js

const foo = require("path/to/foo")
describe("Foo suite", () => {

    it( "Expect foo to greet", () => {

        //This makes the log visible again from the command line.
        spyOn(console, 'log').and.callThrough();

        expect(foo("World", true)).toBe("Hello World");
    });
});

不知道为什么,但spyOn 使日志再次可见。尽管除了调用callThrough 之外,我并没有真正用它做任何事情。我最好的猜测是,通过这样做,console.log 实际上是从 Jasmine 进程中调用的。

【讨论】:

    猜你喜欢
    • 2010-11-17
    • 2012-11-09
    • 2021-09-10
    • 1970-01-01
    • 1970-01-01
    • 2018-12-06
    • 2014-03-31
    • 2019-02-17
    相关资源
    最近更新 更多