2016-06-06 6 views
0

В моем тестовом модуле я должен вернуть обещание, так что мои тесты не выходят из строя:Возвращение обещания в жасминовом контроллер директивы

describe('refugeeRegister', function() { 

    var controller, scope, rootScope, window; 

    beforeEach(function() { 
    module('refugeeHire'); 
    module("templates"); 
    module('refugeeHire.components.refugee_register', function ($provide) { 
     $provide.service("authenticationService", function() { 
     this.signup = jasmine.createSpy("signup").and.returnValue(); 
     this.getCookie=jasmine.createSpy("getCookie"); 
     }); 
    }); 

    }); 

    beforeEach(inject(function ($rootScope, $compile) { 
    el = angular.element("<refugee-register></refugee-register>"); 
    $compile(el)($rootScope.$new()); 
    $rootScope.$digest(); 

    rootScope = $rootScope; 

    controller = el.controller("refugeeRegister"); 

    scope = el.isolateScope() || el.scope(); 

    })); 

    it("should have initialized variables", function() { 
    expect(scope.vm).toBeDefined(); 
    }); 

    it("should register a user", function() { 
    scope.register(); 
    expect(rootScope.globals.loading).toBe(true); 
    expect(authenticationService.signup).toHaveBeenCalled(); 
    }); 

}); 

Он должен проверить этот модуль (с логикой внутри контроллера):

function refugeeRegister() { 
     var directive = { 
      restrict: 'E', 
      templateUrl: 'components/register/refugee/refugee_register.html', 
      scope: {}, 
      controller: refugeeRegisterController, 
      controllerAs: 'vm', 
      bindToController: true 
     }; 

ошибка в тесте следующее:

TypeError: undefined is not an object (evaluating 'authenticationService.signup($scope.vm, "REFUGEE").then') in /Users/fabianlurz/refugeehire/app/components/register/refugee/refugee_register.js (line 73) 
    [email protected]/Users/fabianlurz/refugeehire/app/components/register/refugee/refugee_register.js:73:60 
    /Users/fabianlurz/refugeehire/app/components/register/refugee/refugee_register-spec.js:35:19 
PhantomJS 2.1.1 (Mac OS X 0.0.0): Executed 4 of 4 (1 FAILED) (0.004 secs/0.407 secs) 

Так как же я вернуть обещание в моем mockService (AuthenticationService)?

ответ

1

жестко-закодированных статического разрешения может быть

this.signup = jasmine.createSpy("signup").and.returnValue($q.resolve('...'); 

Для динамического разрешения новое обещание должно быть выдано при каждом вызове:

this.signup = jasmine.createSpy("signup").and.callFake(function() { 
    return $q.resolve(...); 
}); 
+0

«Не удается найти переменную $ д» -> как вводить его на этом этапе? –

+0

Как и с любым другим сервисом, '$ provision.service (" authenticationService ", function ($ q) {...})'. – estus

Смежные вопросы