6

我正在测试一个轮询资源直到满足条件的序列。

Book = $resource("/books/:id", {id: "@id"});

function poll(success) {
  Book.get({id:1}, function() {
    if (canStop) {
       success();
    } else {
       $timeout(poll, 1000);
    }

  });
};

下面的测试失败了Error: Unsatisfied requests: GET /user_workshops/1

describe('poll', function() {
  beforeEach(function() {
    $httpBackend.expectGET('/books/1').respond(200,{id:1});
    $httpBackend.expectGET('/books/1').respond(200,{id:1, newVal:1});

    poll(function() {
       successCalled = true;
    });

    $httpBackend.flush();
    $timeout.flush();

    canStop=true;

    $httpBackend.flush();
  });

  it('should call success when canStop is true', function() {
     expect(successCalled).toBe(true);
  });
});

我尝试重新排列测试顺序,将第二个放在第二个expectGET之前,httpBackend.flush()但后来我得到:

Error: Unexpected request: POST /books/1
No more request expected
4

2 回答 2

11

经过一个小时的拉扯后,我意识到 httpBackend对测试调用的顺序非常具体 - 不仅必须在调用 flush 之前设置期望,而且必须在发出资源请求之前设置期望,并且当你调用 flush 时,你必须已准确且仅提出预期的要求。

这意味着如果你想在顺序请求之间刷新,请求和期望的顺序必须是准确的:

$httpBackend.expectGET('...')
resource.get();
$httpBackend.flush()
$httpBackend.expectGET('...')
resource.get();
$httpBackend.flush()
...
etc

因此,对于上面的代码,如果我将排序更改为:

describe('poll', function() {
  beforeEach(function() {
    $httpBackend.expectGET('/books/1').respond(200,{id:1});

    poll(function() {
       successCalled = true;
    });

    $httpBackend.flush();

    $httpBackend.expectGET('/books/1').respond(200,{id:1, newVal:1});

    $timeout.flush();
    canStop=true;

    $httpBackend.flush();
  });

  it('should call success when canStop is true', function() {
     expect(successCalled).toBe(true);
  });
});
于 2015-07-28T06:16:11.567 回答
0

您还可以在每次调用时重新配置 $httpBackend 方法。有点像:

var deleteMethod = function () {
    $httpBackend.when('DELETE', /.*/gi).respond(function(method, url, data) {
        deleteMethod(); // <--- See here method is rearmed
        return [200, 'OK', {}];
    });
}
deleteMethod();
于 2017-02-06T22:46:45.930 回答