SpyOn自动调用功能吗? - Jasmine / AngularJS

时间:2016-06-02 17:59:08

标签: angularjs jasmine

我无法在文档中找到它指定的位置。这是代码:

beforeEach(inject(function(_$rootScope_, _$q_, _$httpBackend_, _$controller_, _$rootScope_, _ToolsService_) {

    ToolsService = _ToolsService_;

    $q = _$q_;
    deferred = _$q_.defer();
    deferred2 = _$q_.defer();

    $scope = _$rootScope_.$new();

    $httpBackend = _$httpBackend_;

    // these are all called when using $scope.$apply(), so spoof them
    $httpBackend.when('GET', 'http://192.168.99.100/groupReadylift/catalog-modals/please-wait.php').respond({});
    $httpBackend.when('GET', 'http://192.168.99.100/groupReadylift/catalog-modals/custom.php').respond({});
    $httpBackend.when('GET', 'http://192.168.99.100/groupReadylift/catalog-modals/paypal-standby.php').respond({});

    $controller = _$controller_;
    $rootScope = _$rootScope_.$new();
    $rootScope.refreshBalance = function() {};
    $rootScope.user = {};

    $rootScope.validators = {};
    $rootScope.validators.gifting = {};

    spyOn(ToolsService, 'resetForm').and.returnValue(deferred.promise);

    spyOn($rootScope, 'refreshBalance').and.returnValue(deferred2.promise);

    $controller('GiftingController', { $scope: $scope, $rootScope: $rootScope, ToolsService: ToolsService });

}));


it ('Should set $rootScope.errorMessage to "Ineligible to gift points." when calling $rootScope.refreshBalance().', function() {
    $rootScope.user.allowRedemption = false;

    deferred2.resolve([{id:1}]);
    $scope.$apply();

    $rootScope.refreshBalance();

    expect($rootScope.refreshBalance).toHaveBeenCalled();
    expect($rootScope.refreshBalance).toHaveBeenCalledTimes(2);
    expect($rootScope.errorMessage).toEqual("Ineligible to gift points.");
});

要带走的主要事情是toHaveBeenCalledTimes(2)传递,即使我明确地只调用了一次。

么?

0 个答案:

没有答案