简体   繁体   English

如何使用extjs与sencha test模拟ajax调用

[英]how to mock ajax call with sencha test with extjs

I have to veryfiy the response of the ajax call in my sencha test. 我必须在我的sencha测试中非常肯定ajax调用的响应。 plz advise how to do it.. below is my sample code 请建议如何做..以下是我的示例代码

beforeEach(()=> {

    sim = Ext.ux.ajax.SimManager.init({});
    controller = Ext.create('xxxx.controller.Search');
    AutoLink = Ext.create('xxxx.model.search.AutoLink', {
        objectType: 'myobj'
    });
});
it('Should run processResponse when doSearch executes', function() {
    const callback = () => {};

    sim.register({
        'abc.com/myurl.aspx': {
            status: 401,
            responseText: JSON.stringify({
                'success': true,
                'data': [{
                    'autoLink': false, 'status': 'OK', 'objectType': 'Person',
                    'results': [{ 'ref': 12345, 'managedBy': '01', 'ethnicAppearance': '1', 'gender': '1', 'rules': ['Forename, surname','nickname, DOB']}],
                    'gridDescriptor': [{'fields': [{'name': 'surname','text': 'Surname','width': 100}],
                        'sortOrders': ['surname','forename1']
                    }]
                }]
            })
        }          
    });

    spyOn(controller, 'doSearch'); // internal method which calls the Ext.Ajax
    spyOn(controller, 'processResponse'); // internal method which process the response        

    controller.doSearch(AutoLink, callback, this); // making an ajax call

    setTimeout(function () {
        expect(controller.processResponse).toHaveBeenCalled();
    }, 1000);
});

now when run the test case processResponse gets called, which is fine, but i want to verify the ajax response. 现在,当运行测试用例processResponse被调用时,这很好,但我想验证ajax响应。

This is how I am doing it: 这就是我的做法:

$.ajax({
                           url: _spPageContextInfo.webServerRelativeUrl + "/_api/web/lists/getbytitle('Test%203')/items(" + itemId + ")/FieldValuesAsText",
                           method: 'GET',
                           headers: {
                               'accept': 'application/json;odata=verbose'
                           }
                       }).then(function (data) {
                           console.log(data);
                          }

I don't know if this will help you to achieve exactly what you are looking for. 我不知道这是否可以帮助您实现所需的目标。 But I would suggest giving it a shot. 但是我建议试一试。 Then you can go to your console and save the data object to a variable (just for debugging purposes) or just from the console itself look at the object chain and check the data which was returned by your ajax call. 然后,您可以转到控制台并将数据对象保存到变量中(仅用于调试目的),或者仅从控制台本身查看对象链并检查ajax调用返回的数据。 So in my case I would find let's say name of the employee here:- data.d.results[0].PreferredName. 因此,在我的情况下,我会在这里找到员工的姓名:-data.d.results [0] .PreferredName。 Then if I want to use it I can just save it in a variable. 然后,如果我想使用它,我可以将其保存在变量中。 Make sure you do it in the 'then' function. 确保在“然后”功能中执行此操作。 Here's a sample for save the name to a var: 这是将名称保存到var的示例:

.then(function (data) {
     empName = data.d.results[0].PreferredName;
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM