我正在继承一些代码,我有两个他们的测试仍然失败,不确定它们是不是以前的,或者是因为我有不同版本的 Jasmine(它们是 2.0 之前的)
失败的测试在 beforeEach 中有这个间谍设置
spyOn(datacontext, 'getImportLogForDate').and.callThrough();
Run Code Online (Sandbox Code Playgroud)
然后在测试中
controller.DepositDate = new Date();
controller.PerformActionThatCallsGetImportLogForDate();
expect(context.getImportLogForDate).toHaveBeenCalledWith('1', controller.DepositDate);
Run Code Online (Sandbox Code Playgroud)
由此产生的错误令人困惑,因为它们是相同的
预期 spy getImportLogForDate 已被调用['1', Date(Thu Dec 04 2014 13:00:51 GMT-0600 (Central Standard Time))]但实际调用是['1', Date(Thu Dec 04 2014 13) :00:51 GMT-0600(中部标准时间))]。
我不能验证使用日期调用的函数吗?
PerformActionThatCallsGetImportLogForDate日期对象在做什么?Jasmine 按毫秒值比较日期对象,因此即使相差 1 毫秒,它们也不会相等,但仅读取控制台输出,您将看不到该级别的详细信息。
或者,您还有 2 个其他选择。
只需测试日期对象是否用作第二个参数。
expect(context.getImportLogForDate)
.toHaveBeenCalledWith('1', jasmine.any(Date));
Run Code Online (Sandbox Code Playgroud)
测试该日期值,但在 a 之外toHaveBeenCalledWith,以防该匹配器出现某些特定的异常情况。
expect(context.getImportLogForDate.calls.mostRecent().args[0])
.toEqual('1');
expect(context.getImportLogForDate.calls.mostRecent().args[1])
.toEqual(controller.DepositDate);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3232 次 |
| 最近记录: |