Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/478.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在sinon中模拟当前时间_Javascript_Testing_Sinon_Stub - Fatal编程技术网

Javascript 在sinon中模拟当前时间

Javascript 在sinon中模拟当前时间,javascript,testing,sinon,stub,Javascript,Testing,Sinon,Stub,我正在为使用连接到MongoDB的应用程序编写集成测试。我将实体创建时间写入DB,并使用Date.now()实现该功能。 我的应用程序是时间敏感的,因此我想模拟当前时间,以便我的测试始终有效。 我曾尝试过在多个其他类似帖子上分享的例子,但无法为我找到有效的解决方案 我尝试添加 const date = new Date() date.setHours(12) sandbox.stub(Date, "now").callsFake(function() {return date.getTime()

我正在为使用连接到MongoDB的应用程序编写集成测试。我将实体创建时间写入DB,并使用
Date.now()
实现该功能。 我的应用程序是时间敏感的,因此我想模拟当前时间,以便我的测试始终有效。 我曾尝试过在多个其他类似帖子上分享的例子,但无法为我找到有效的解决方案

我尝试添加

const date = new Date()
date.setHours(12)
sandbox.stub(Date, "now").callsFake(function() {return date.getTime()})
在我的
beforeach
方法中,但它没有影响

我也试过了

const date = new Date()
date.setHours(12)

sinon.useFakeTimers({
    now: date,
    shouldAdvanceTime: true
})
但这会让我的猫鼬模式验证陷入困境

架构配置无效:
ClockDate
在路径
createdDate


实现这一点的正确方法是什么?

这里有一种为
日期创建存根的方法。现在()

main.ts

export function main(){
返回日期。现在();
}
main.test.ts

从“/main”导入{main};
从“sinon”进口sinon;
从“chai”导入{expect};
描述(“59635513”,()=>{
之后(()=>{
sinon.restore();
});
它(“应该通过”,()=>{
常数mDate=1000*1000;
const dateNowStub=sinon.stub(Date,“now”).returns(mDate);
常量实际值=main();
预期(实际).to.be.eq(mDate);
sinon.assert.calledOnce(dateNowStub);
});
});
单元测试结果和覆盖率报告:


59635513
✓ 应该通过
1次通过(8毫秒)
--------------|----------|----------|----------|----------|-------------------|
文件|%Stmts |%Branch |%Funcs |%Line |未覆盖行|s|
--------------|----------|----------|----------|----------|-------------------|
所有文件| 100 | 100 | 100 | 100 ||
main.test.ts | 100 | 100 | 100 | 100 ||
main.ts | 100 | 100 | 100 | 100 ||
--------------|----------|----------|----------|----------|-------------------|

源代码:

这里有一种为日期创建存根的方法。现在():

main.ts

export function main(){
返回日期。现在();
}
main.test.ts

从“/main”导入{main};
从“sinon”进口sinon;
从“chai”导入{expect};
描述(“59635513”,()=>{
之后(()=>{
sinon.restore();
});
它(“应该通过”,()=>{
常数mDate=1000*1000;
const dateNowStub=sinon.stub(Date,“now”).returns(mDate);
常量实际值=main();
预期(实际).to.be.eq(mDate);
sinon.assert.calledOnce(dateNowStub);
});
});
单元测试结果和覆盖率报告:


59635513
✓ 应该通过
1次通过(8毫秒)
--------------|----------|----------|----------|----------|-------------------|
文件|%Stmts |%Branch |%Funcs |%Line |未覆盖行|s|
--------------|----------|----------|----------|----------|-------------------|
所有文件| 100 | 100 | 100 | 100 ||
main.test.ts | 100 | 100 | 100 | 100 ||
main.ts | 100 | 100 | 100 | 100 ||
--------------|----------|----------|----------|----------|-------------------|

源代码:

使用的
useFakeTimers
正在做它打算做的事情。但是,mongoose类型比较失败,因为它通过检查名称进行类型比较。 解决方案是将
ClockDate
作为已知类型添加到沙箱中的mongoose中

在方法起作用之前,在
中添加以下行

import { SchemaTypes } from "mongoose"
SchemaTypes["ClockDate"] = SchemaTypes.Date

useFakeTimers
的使用是在做它打算做的事情。但是,mongoose类型比较失败,因为它通过检查名称进行类型比较。 解决方案是将
ClockDate
作为已知类型添加到沙箱中的mongoose中

方法起作用之前,在
中添加以下行

import { SchemaTypes } from "mongoose"
SchemaTypes["ClockDate"] = SchemaTypes.Date