简体   繁体   English

如何在茉莉花中测试.then()?

[英]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. 您应该只使用it方法的回调提供的参数。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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