Javascript Jasmine错误:预期已使用[[]调用spy decode,但实际调用[未定义]

Javascript Jasmine错误:预期已使用[[]调用spy decode,但实际调用[未定义],javascript,extjs,jasmine,Javascript,Extjs,Jasmine,我正在尝试学习ExtJS和Jasmine框架的单元测试。我写了这个方法,我想测试decode方法是否被某个值调用,但我一直得到标题中提到的错误。我做错了什么 方法: onSuccess: function (response) { var text = Ext.decode(response.responseText); this.someGrid.store.loadRawData(text); } 茉莉花规格: it('Function will call decode m

我正在尝试学习ExtJS和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()将返回空数组-如spy中设置的那样。这个空数组将存储在文本变量中。然后,它将被传递到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.decode(),因此当函数尝试访问responseText属性时,它接收到未定义的内容

// var response = {}; - empty object created in your test
Ext.decode(response.responseText);
在onSuccess函数中,调用returnValue()将返回空数组-如spy中设置的那样。这个空数组将存储在文本变量中。然后,它将被传递到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.decode(),因此当函数尝试访问responseText属性时,它接收到未定义的内容

// var response = {}; - empty object created in your test
Ext.decode(response.responseText);
在onSuccess函数中,调用returnValue()将返回空数组-如spy中设置的那样。这个空数组将存储在文本变量中。然后,它将被传递到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.decode(),因此当函数尝试访问responseText属性时,它接收到未定义的内容

// var response = {}; - empty object created in your test
Ext.decode(response.responseText);
在onSuccess函数中,调用returnValue()将返回空数组-如spy中设置的那样。这个空数组将存储在文本变量中。然后,它将被传递到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([]);
})

我不认为这个测试特别有用。这更多的是测试框架的内部结构。您最好测试存储中的数据是否与您认为应该匹配。我不认为此测试特别有用。这更多的是测试框架的内部结构。您最好测试存储中的数据是否与您认为应该匹配。我不认为此测试特别有用。这更多的是测试框架的内部结构。您最好测试存储中的数据是否与您认为应该匹配。我不认为此测试特别有用。这更多的是测试框架的内部结构。您最好测试存储中的数据是否与您认为应该匹配。