Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/458.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Jasmine,一个测试,多个ajax请求(错误:ajax已经被监视)_Javascript_Jquery_Ajax_Jasmine - Fatal编程技术网

Javascript Jasmine,一个测试,多个ajax请求(错误:ajax已经被监视)

Javascript Jasmine,一个测试,多个ajax请求(错误:ajax已经被监视),javascript,jquery,ajax,jasmine,Javascript,Jquery,Ajax,Jasmine,我有一个Jasmine 2.0.2测试,它会触发一个ajax请求,但是每次触发请求时,模拟ajax返回都应该是一个特定的返回值 var setUpDeleteEventInAjax = function(spyEvent, idToReturn){ var spy; spy = jasmine.createSpy('ajax'); spyAjaxEvent = spyOnEvent(spyEvent, 'click'); spyOn($, 'ajax').an

我有一个Jasmine 2.0.2测试,它会触发一个ajax请求,但是每次触发请求时,模拟ajax返回都应该是一个特定的返回值

  var setUpDeleteEventInAjax = function(spyEvent, idToReturn){
    var spy;
    spy = jasmine.createSpy('ajax');
    spyAjaxEvent = spyOnEvent(spyEvent, 'click');
    spyOn($, 'ajax').and.callFake(function (param) {
      return {
        id: idToReturn,  // here I am trying to return a defined value
        status: true
      };
    });
    spyAjaxEvent.reset();  //  this should reset all ajax evetns
  };

...
beforeEach(function(){...})
afterEach(function(){...})
...

it('Deleting all the addresses should reveal the form', function () {

    setUpDeleteEventInAjax('#delete',52670);
    $('#delete').click();
    expect($('.address-item').length).toEqual(4);

    setUpDeleteEventInAjax('#delete-2',52671);
    $('#delete-2').click();
    expect($('.address-item').length).toEqual(2);

    setUpDeleteEventInAjax('#delete-3',52672);
    $('#delete-3').click();
    expect($('.address-item').length).toEqual(0);

  });
...
单击delete按钮(delete、delete-2、delete-3)后,地址项的总长度将减少2(当服务器返回的值以数字响应时,这是模拟的关键)


然而,jasmine抱怨说“ajax已经被监视了”。有没有办法从ajax模拟中返回一个新的值以完成测试?

实际上,我所采用的方法是过度设计的。只是需要一个了结。不需要我的自定义
setUpDeleteEventInAjax
函数

  it('Deleting all the addresses should reveal the form', function () {
    var responses = [52670, 52671, 52672];
    var ajaxResponses = function () {
      return {
        status: true,
        id: responses.shift()
      }
    };
    spyOn($, 'ajax').and.callFake(ajaxResponses);

    $('#delete').click();
    expect($('.address-item').length).toEqual(4);

    $('#delete-2').click();
    expect($('.address-item').length).toEqual(2);
    expect($('.address-book')).toHaveClass('single-address');

    $('#delete-3').click();
    expect($('.address-item').length).toEqual(0);

  });

顺便说一句,如果您将响应程序函数设置为
返回{status:true,id:responses.shift()}
您不需要该
callCount
变量。