1
PhantomJS 2.1.1 (Windows 7 0.0.0)e Service should get by id from Service FAILED
    Error: Unexpected request: GET /users/currentuser
    No more request expected in /static/third-party/angularjs/1.4.7/js/angular-mocks.js (line 1245)

这是我收到的服务错误消息。在添加 angulartics 之前,我不会收到这些消息

有没有办法确保这些错误不会发生

我的测试代码:

beforeEach(function() {

    module('appHost');
    module('angulartics');

    inject(function($httpBackend, _Service_) {

        Service= _Service_;
        httpBackend = $httpBackend;
    });

});

afterEach(function() {
    httpBackend.verifyNoOutstandingExpectation();
    httpBackend.verifyNoOutstandingRequest();
});

it('should get template by id from Service', function() {

    var expected = {
            "name": "my name",
            "description": "bla bla bla bla",

        };

    var id = "12345";
    httpBackend.expectGET("/data/"+id).respond(expected);
        });
4

2 回答 2

1

我也经历过类似的事情。我的解决方法只是添加这一行

httpBackend.whenGET(/users/currentuser/).respond(200, '');

在每个块之前。然后我得到了另一个错误,因为 Angular 试图获取它自己的模板。我通过更改业力预处理器配置以去除有问题的前缀来解决这个问题。如果您需要更多帮助,请告诉我。

于 2016-08-01T19:55:59.410 回答
0

尝试添加httpBackend.flush()

it('should get template by id from Service', function() {

    var expected = {
            "name": "my name",
            "description": "bla bla bla bla",

        };

    var id = "12345";
    httpBackend.expectGET("/data/"+id).respond(expected);
    httpBackend.flush()
});
于 2016-08-01T20:19:34.823 回答