【发布时间】:2014-02-07 00:25:17
【问题描述】:
我有一个在 IE8 中失败的脚本,因为 Date.now() 未定义。我已将 Date.now() 抽象为一个方法并想对其进行单元测试,我知道它在 Date.now() 未定义时是否有效。
Foo.prototype = {
date: function() {
// I think this works. Not sure until I get my unit test working...
if (typeof Date.now === 'undefined') {
Date.now = function () { return +new Date(); }
}
return Date.now(); // fails in IE8. Undefined.
}
}
我的测试是这样的
describe('Foo', function() {
it('has the current date time', function() {
// This passes
spyOn(Date, 'now').and.returnValue(1234);
foo = new Foo;
expect(foo.date()).toBe(1234);
});
it('has the current date time for IE8', function() {
// This won't pass
spyOn(Date, 'now').and.returnValue(null);
foo = new Foo;
expect(foo.date()).toBe(1234); // TODO
});
});
如何存根未定义?我正在考虑在 returnValue 中返回一个未定义的方法。
我知道我的测试并没有完善,因为我才刚刚开始使用 Jasmine。
【问题讨论】:
标签: unit-testing tdd jasmine