如何在 Karma 单元测试中解析控制器中的 $q.all?
How can I resolve a $q.all in my controller in a Karma unit test?
我的控制器有:
switchUserAccount: function() {
$scope.model.currentMode = 'user';
console.log(ipCookie('currentPatientId'));
$q.all([facilityCache.getFacility(), facilityGroupCache.getGroupList(), languageCache.getLanguageList(), genderCache.getGenderList(), raceCache.getRaceList(), dosingCache.getDosingOptions()])
.then(function(){
console.log('back from then');
cache.set('ui', 'adminPage', '');
cache.set('ui', 'schedulePage', 'patients');
if(ipCookie('currentPatientId')) {
$location.path('/patient/view/' + ipCookie('currentPatientId'));
} else {
$location.path('/patients');
}
});
},
我的测试是
describe('MainHeaderController', function() {
var scope, $rootScope, $locationMock, $httpBackend;
beforeEach(function() {
module('mapApp');
return inject(function($injector) {
var $controller, $q, ipCookieMock;
$rootScope = $injector.get('$rootScope');
$controller = $injector.get('$controller');
$httpBackend = $injector.get('$httpBackend');
$q = $injector.get('$q');
$locationMock = jasmine.createSpyObj('$location', ['path'])
ipCookieMock = function() {
return 123;
}
scope = $rootScope.$new()
$controller('MainHeaderController', {
$scope: scope,
$location: $locationMock,
$q: $q,
ipCookie: ipCookieMock
});
$httpBackend.whenGET('/rpc/session').respond(200);
$httpBackend.whenPOST('/rpc').respond(200);
return scope.$digest();
});
});
it('should redirect to a patient view if a cookie is set', function($rootScope) {
scope.switchUserAccount();
// $rootScope.$apply();
expect($locationMock.path).toHaveBeenCalledWith('/patient/view/123');
});
});
所以我期望发生的是 $location.path
被 /patient/view/123
调用。相反,我得到的是
Expected spy $location.path to have been called with [ '/patient/view/123' ] but actual calls were [ ].
如果我取消注释 $rootScope.$apply()
,我会得到
TypeError: 'undefined' is not a function (evaluating '$rootScope.$apply()')
那么如何触发控制器中的 $q.all
以便测试能够正常通过?
谢谢!
您通过将其声明为测试函数的参数来隐藏测试套件的 $rootScope
变量。这就是它未定义的原因:茉莉花调用带有任何参数的测试函数。
替换
it('should redirect to a patient view if a cookie is set', function($rootScope) {
来自
it('should redirect to a patient view if a cookie is set', function() {
我的控制器有:
switchUserAccount: function() {
$scope.model.currentMode = 'user';
console.log(ipCookie('currentPatientId'));
$q.all([facilityCache.getFacility(), facilityGroupCache.getGroupList(), languageCache.getLanguageList(), genderCache.getGenderList(), raceCache.getRaceList(), dosingCache.getDosingOptions()])
.then(function(){
console.log('back from then');
cache.set('ui', 'adminPage', '');
cache.set('ui', 'schedulePage', 'patients');
if(ipCookie('currentPatientId')) {
$location.path('/patient/view/' + ipCookie('currentPatientId'));
} else {
$location.path('/patients');
}
});
},
我的测试是
describe('MainHeaderController', function() {
var scope, $rootScope, $locationMock, $httpBackend;
beforeEach(function() {
module('mapApp');
return inject(function($injector) {
var $controller, $q, ipCookieMock;
$rootScope = $injector.get('$rootScope');
$controller = $injector.get('$controller');
$httpBackend = $injector.get('$httpBackend');
$q = $injector.get('$q');
$locationMock = jasmine.createSpyObj('$location', ['path'])
ipCookieMock = function() {
return 123;
}
scope = $rootScope.$new()
$controller('MainHeaderController', {
$scope: scope,
$location: $locationMock,
$q: $q,
ipCookie: ipCookieMock
});
$httpBackend.whenGET('/rpc/session').respond(200);
$httpBackend.whenPOST('/rpc').respond(200);
return scope.$digest();
});
});
it('should redirect to a patient view if a cookie is set', function($rootScope) {
scope.switchUserAccount();
// $rootScope.$apply();
expect($locationMock.path).toHaveBeenCalledWith('/patient/view/123');
});
});
所以我期望发生的是 $location.path
被 /patient/view/123
调用。相反,我得到的是
Expected spy $location.path to have been called with [ '/patient/view/123' ] but actual calls were [ ].
如果我取消注释 $rootScope.$apply()
,我会得到
TypeError: 'undefined' is not a function (evaluating '$rootScope.$apply()')
那么如何触发控制器中的 $q.all
以便测试能够正常通过?
谢谢!
您通过将其声明为测试函数的参数来隐藏测试套件的 $rootScope
变量。这就是它未定义的原因:茉莉花调用带有任何参数的测试函数。
替换
it('should redirect to a patient view if a cookie is set', function($rootScope) {
来自
it('should redirect to a patient view if a cookie is set', function() {