Jasmine Error: Expected spy decode to have been called with [ [ ] ] but actual calls were [ undefined ]
Jasmine Error: Expected spy decode to have been called with [ [ ] ] but actual calls were [ undefined ]
我正在尝试学习 Ext JS 和使用 Jasmine 框架进行单元测试。我写了这个方法,我想测试 decode
方法是否以特定值被调用,但我不断收到标题中提到的错误。我在这里做错了什么?
方法:
onSuccess: function (response) {
var text = Ext.decode(response.responseText);
this.someGrid.store.loadRawData(text);
}
茉莉规格:
it('Function will call decode method', function () {
var response = {};
spyOn(Ext, 'decode').and.returnValue([]);
me.testObj.onSuccess(response);
expect(Ext.decode).toHaveBeenCalledWith([]);
})
您正在向函数 Ext.decode() 传递一个空对象,因此当函数尝试访问 responseText 属性 它接收未定义。
// var response = {}; - empty object created in your test
Ext.decode(response.responseText);
在您的 onSuccess 函数中,对 returnValue() 的调用将 return 空数组 - 正如在您的间谍中设置的那样。这个空数组将存储在文本变量中。然后它将被传递给 loadRawData() 函数,而不是像你的间谍当前期望的那样 decode()。
var text = Ext.decode(response.responseText);
this.someGrid.store.loadRawData(text);
为了正确测试函数,您可以在测试中模拟响应对象以包含 responseText 属性。您还可以为 loadRawData() 函数添加 spy 和 expect 语句,如下所示:
it('Function will call decode method', function () {
// mock response object has responseText propety
var response = { responseText: 'mockResponseText' };
spyOn(Ext, 'decode').and.returnValue([]);
// spy to LoadRawData added to check return value of decode is passed on correctly
spyOn(me.testObj.someGrid.store, 'loadRawData');
me.testObj.onSuccess(response);
// Ext.decode should be called with the response text
expect(Ext.decode).toHaveBeenCalledWith('mockResponseText');
// loadRawData should be called with return value of decode function
expect(me.testObj.someGrid.store.loadRawData).toHaveBeenCalledWith([]);
})
我正在尝试学习 Ext JS 和使用 Jasmine 框架进行单元测试。我写了这个方法,我想测试 decode
方法是否以特定值被调用,但我不断收到标题中提到的错误。我在这里做错了什么?
方法:
onSuccess: function (response) {
var text = Ext.decode(response.responseText);
this.someGrid.store.loadRawData(text);
}
茉莉规格:
it('Function will call decode method', function () {
var response = {};
spyOn(Ext, 'decode').and.returnValue([]);
me.testObj.onSuccess(response);
expect(Ext.decode).toHaveBeenCalledWith([]);
})
您正在向函数 Ext.decode() 传递一个空对象,因此当函数尝试访问 responseText 属性 它接收未定义。
// var response = {}; - empty object created in your test
Ext.decode(response.responseText);
在您的 onSuccess 函数中,对 returnValue() 的调用将 return 空数组 - 正如在您的间谍中设置的那样。这个空数组将存储在文本变量中。然后它将被传递给 loadRawData() 函数,而不是像你的间谍当前期望的那样 decode()。
var text = Ext.decode(response.responseText);
this.someGrid.store.loadRawData(text);
为了正确测试函数,您可以在测试中模拟响应对象以包含 responseText 属性。您还可以为 loadRawData() 函数添加 spy 和 expect 语句,如下所示:
it('Function will call decode method', function () {
// mock response object has responseText propety
var response = { responseText: 'mockResponseText' };
spyOn(Ext, 'decode').and.returnValue([]);
// spy to LoadRawData added to check return value of decode is passed on correctly
spyOn(me.testObj.someGrid.store, 'loadRawData');
me.testObj.onSuccess(response);
// Ext.decode should be called with the response text
expect(Ext.decode).toHaveBeenCalledWith('mockResponseText');
// loadRawData should be called with return value of decode function
expect(me.testObj.someGrid.store.loadRawData).toHaveBeenCalledWith([]);
})