簡體   English   中英

如何在茉莉花中測試.then()?

[英]how to test a .then() 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);
};

到目前為止,我已經提出了

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

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

它通過了,但是我對如何測試承諾的錯誤和成功部分感到困惑

您應該只使用it方法的回調提供的參數。

這是一個有效的例子

    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