我尝试为下面的方法编写适当的单元测试。我正在尝试在格式化日期内测试断言。因此尝试使用 Ext.Date.format断言中的方法,但返回为失败。
如何将给定日期和预期日期格式化为 d.m.Y
? (我不允许更改方法本身!)
dateAdd: function (aDate, aInterval, aValue) {
var me = this;
aDate = aDate || me.today();
aInterval = aInterval || Ext.Date.DAY;
aValue = aValue || 1;
return Ext.Date.add(aDate, aInterval, aValue);
},
和测试断言;
t.describe('dateAdd() method', function(t) {
t.it('should interval the given date', function(t) {
t.expect(MyApp.dateAdd(new Date(Ext.Date.format('01.01.2018', 'd.m.Y')), Ext.Date.DAY, 1)).toBe('02.01.2018');
});
});
// Result:
// Failed assertion `expect(got).toBe(need)` at line 64 of 01-unit-tests.js
// Got : "Invalid Date"
// Need : "02.01.2018"
最佳答案
根据 ExtJs 文档,Ext.Date.add返回一个 Date 对象,您将其与 String 进行比较。 当您将 String 与 Date 对象交换时,它应该可以工作。
t.describe('dateAdd() method', function(t) {
t.it('should interval the given date', function(t) {
var expectedDate = Ext.Date.format('02.01.2018', 'd.m.Y');
t.expect(MyApp.dateAdd(new Date(Ext.Date.format('01.01.2018', 'd.m.Y')), Ext.Date.DAY, 1)).toBe(expectedDate);
});
});
关于javascript - 如何使用 Ext JS 格式化方法在单元测试期间格式化日期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48110688/