简体   繁体   中英

how to test a .then() in jasmine ?

I have the following piece of code which I am trying to test in jasmine

$scope.createTeam = function(team) {
var errorCB, successCB;
  successCB = function(resp) {
    return $scope.followRepository(resp.team, true);
  };
  errorCB = function(err) {
    return toaster.pop('error', 'Team Not Created', err);
  };
  return TeamService.createTeam(team).then(successCB, errorCB);
};

So far I have come up with

this.TeamServiceSpy2 = spyOn(this.TeamService, 'createTeam').and.callThrough();

it("should create a team", function() {
  return this.scope.createTeam(this.teamMock).expect(this.TeamServiceSpy2).toHaveBeenCalled();
}); 

and it passed but I am confused on how to test the error and success part of the promise

You should simply use the parameter provided by the callback of the it method.

here is a working example

    it('should get a record', function(done) {
        var self = this;
        return user.get(id)
            .then(function(response) {
                expect(response.email).toEqual('test@test.com');
                done();
            })
            .catch(function(error) {
                self.fail(error);
                done();
            });
    });

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