简体   繁体   中英

Jasmine unit tests fail after adding angulartics with the angulartics-piwik

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)

this is the error message i receive for my services. Before adding angulartics i wouldn't get those messages

Is there any way to make sure these errors dont happen

my testing code :

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

I also experienced something similar. My fix was simply to add this line

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

in the before each block. Then I got another error as angular was trying to get it's own templates. I fixed that by making changes to the karma pre-processor config to strip the prefix in question. Let me know if you need any more help on this.

Try to add 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()
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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