使用angulartics-piwik添加angulartics后,Jasmine单元测试失败

时间:2016-04-28 13:41:51

标签: angularjs jasmine karma-jasmine angulartics

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);
        });

2 个答案:

答案 0 :(得分:1)

我也经历过类似的事情。我的修复只是添加这一行

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

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

答案 1 :(得分:0)

尝试添加<div class="medium-2 column first-post-category"> <h1 class="category-first">Hip Hop</h1> </div>

httpBackend.flush()