在jasmine指令控制器中返回一个promise [英] Return a promise in jasmine directive controller
本文介绍了在jasmine指令控制器中返回一个promise的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的单元测试中,我需要返回一个承诺,以便我的测试不会失败:
In my unit test i need to return a promise so that my tests don't fail:
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();
});
});
它应该测试这个模块(控制器内有逻辑):
It should test this module (with logic inside the controller):
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)
register@/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)中返回一个promise?
So how do i return a promise in my mockService (authenticationService)?
推荐答案
对于硬编码的静态分辨率,它可能是
For hard-coded static resolution it may be
this.signup = jasmine.createSpy("signup").and.returnValue($q.resolve('...');
对于动态解决方案,应该发布新的承诺d每次通话:
For dynamic resolution a new promise should be issued on each call:
this.signup = jasmine.createSpy("signup").and.callFake(function () {
return $q.resolve(...);
});
这篇关于在jasmine指令控制器中返回一个promise的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文