我有以下功能和规格。我在日期对象上创建了间谍,我在我的函数中传递和操作。但间谍不是在rightDate上创建的。你能帮助我解决这个问题吗?
var jasmineFunc = new function(leftDate, rightDate){
var leftDateFormat= leftDate.format("H:mm");
rightDate= new Date(rightDate- 60000);
var rightDateFormat = rightDate.format("H:mm");
}
Spec:
describe("format", function(){
var leftDate = new Date(2016, 0, 1, 0, 0, 0, 0);
var rightDate= new Date(2016, 0, 1, 2, 0, 0, 0);
spyOn(leftDate,"format").and,returnValue("00:00");
spyOn(rightDate, "format").and.returnValue("00:59");
});
答案 0 :(得分:0)
您的代码存在一些错误。
但是我嘲笑了方法for your reference and it works
var jasmineFunc = function(leftDate, rightDate) {
var leftDateFormat = leftDate.format("H:mm");
rightDate = new Date(rightDate - 60000);
var rightDateFormat = rightDate.format("H:mm");
}
Date.prototype.format = function() {
return "This formats the text";
}
describe("format", function() {
it('tempSpec', function() {
var leftDate = new Date(2016, 0, 1, 0, 0, 0, 0);
var rightDate = new Date(2016, 0, 1, 2, 0, 0, 0);
spyOn(leftDate, "getHours").and.returnValue("00:00");
spyOn(rightDate, "getHours").and.returnValue("00:59");
spyOn(leftDate, "format").and.returnValue("I've hijacked it in the spy");
var dt = leftDate.getHours()
var txt = leftDate.format()
expect(dt).toEqual("00:00");
expect(txt).toEqual("I've hijacked it in the spy");
})
});
以下是代码的更新版本,我想我终于明白你的意思是对象被改变了,它被重新分配。 那改变了什么?
var jasmineFunc = {
testFunc: function(leftDate, rightDate) {
var leftDateFormat = this.formatFunc(leftDate);
rightDate = new Date(rightDate - 60000);
var rightDateFormat = this.formatFunc(rightDate);
return {'left' : leftDateFormat, 'right' : rightDateFormat}
},
formatFunc: function(value) {
return value.format("H:mm")
}
}
Date.prototype.format = function() {
return "This formats the text";
}
describe("format Spec", function() {
it('tempSpec', function() {
var leftDate = new Date(2016, 0, 1, 0, 0, 0, 0);
var rightDate = new Date(2016, 0, 1, 2, 0, 0, 0);
spyOn(jasmineFunc, "formatFunc").and.returnValue("Some arbitrary Value");
var testObj = jasmineFunc.testFunc(leftDate, rightDate);
expect(testObj.left).toEqual("Some arbitrary Value");
expect(testObj.right).toEqual("Some arbitrary Value");
});
});