$httpBackend in AngularJs Jasmine unit test

后端 未结 1 641
野性不改
野性不改 2021-01-05 11:03

I\'m unable to get my unit test to work properly. I have a $scope array that starts out empty, but should be filled with an $http.get(). In the real environment, there\'d be

1条回答
  •  北海茫月
    2021-01-05 11:39

    You need to place httpLocalBackend.flush() statement before expect($scope.stuff.length).toBe(2). Once you make a request you need to flush it for the data to be available in your client code.

    it('should get stuff', function () {
        var url = '/api/roles';
        var httpResponse = [{ "stuffId": 1 }, { "stuffId": 2 }];
        scope.getStuff(); //Just moved this from after expectGET
        httpLocalBackend.expectGET(url).respond(200, httpResponse);
        httpLocalBackend.flush();
        expect($scope.stuff.length).toBe(2);
    } );
    

    Try this.

    0 讨论(0)
提交回复
热议问题