Mig*_*g82 6 node.js jasmine console.log
我开发了一个 Node.js 模块,并使用 Jasmine 为其编写单元测试。当调用参数设置为 true时,该模块console.log会在执行时打印出一些信息。verbose
假设该模块如下所示:
foo.js
function foo(arg1, verbose){
var output = "Hello " + arg1;
if(verbose)console.log("Returning %s", output);
return output;
}
module.exports = foo;
Run Code Online (Sandbox Code Playgroud)
假设我的测试如下所示:
foo-spec.js
const foo = require("path/to/foo")
describe("Foo suite", () => {
it( "Expect foo to greet", () => {
expect(foo("World", true)).toBe("Hello World");
});
});
Run Code Online (Sandbox Code Playgroud)
jasmine我通过在终端中输入以下内容来运行测试:
$ jasmine
Run Code Online (Sandbox Code Playgroud)
一切正常,除了我想看到详细的输出。
$ jasmine
Returning Hello World
Run Code Online (Sandbox Code Playgroud)
有没有办法让茉莉这样做?
好吧,我已经找到了解决这个问题的方法。监视以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");
});
});
Run Code Online (Sandbox Code Playgroud)
不知道为什么,但spyOn正在使日志再次可见。尽管除了调用之外我并没有真正用它做任何事情callThrough。我最好的猜测是,通过这样做,console.log实际上是从 Jasmine 进程中调用的。
| 归档时间: |
|
| 查看次数: |
8463 次 |
| 最近记录: |