我刚开始使用Node,我现在正在编写一些单元测试.对于前几个功能我可以正常工作,但我现在遇到了一个包含moment.utc()在其中的功能.我的函数的简化版本如下所示:
function calculate_x(positions, risk_free_interest){
let x = 0;
for (let position of positions) {
let expiry_in_years = get_expire_in_years(moment.utc());
if (expiry_in_years > 0){
let pos_x = tools.get_x(expiry_in_years, risk_free_interest);
x += pos_x;
}
}
return x;
}
Run Code Online (Sandbox Code Playgroud)
我尝试使用基本节点断言测试lib来测试它:
"use strict";
const assert = require('assert');
let positions = [{this: 'is', a: 'very', large: 'object'}];
assert.strictEqual(calculate_x(positions, 1.8), 1.5);
Run Code Online (Sandbox Code Playgroud)
由于运行它的时间(以及结果)总是不同,因此总是会失败.
在Python中,我可以设置模拟类和对象.有没有办法让我可以在Node中解决这个问题而不将moment.utc()作为calculate_x()函数的参数?