Angular - 未在 'it' 函数的回调中传递 $controller 未通过测试,为什么?
Angular - Not passing $controller in callback of 'it' function fails test, why?
这里有点测试新手,我正在尝试为控制器编写一些 Jasmine/Karma 测试。我得到了一个示例测试来使用和构建,在这里,$controller
被传递到 it
块的函数的参数中。这会导致测试顺利通过。
我已经成功地为这个应用程序的其他控制器编写了测试,但我还没有看到 $controller
在那里通过。所以我试着按照我所知道的去做,但是当我删除 $controller
时,测试失败了,因为它正在寻找依赖项(我应该通过 beforeEach
中的 $provide.value
提供依赖项)。 ..?)。
这是控制器代码 -
(function() {
'use strict';
angular
.module('myApp')
.controller('EntryAddController', EntryAddController);
function EntryAddController($scope, $state, moment, $sanitize, dogList, catList, months, frequencies, EntryFactory, $modal, toastr, $log) {
var vm = this;
var now = moment();
var tomorrow = now.add(1, 'days').toDate();
var to = now.subtract(1, 'days').add(12, 'months').toDate();
vm.fromDay = tomorrow;
vm.minDay = tomorrow;
start();
function start() {
//some logic here
}
})();
这是我得到的示例代码(注意 it
块)-
(function() {
'use strict';
describe('Entry Add Controller', function(){
describe('EntryAddController', function() {
var vm, $rootScope, $controller;
beforeEach(module('myApp'));
beforeEach(function() {
inject(function(_$rootScope_, _$controller_) {
$rootScope = _$rootScope_;
$controller = _$controller_;
});
});
describe('EntryAddController', function() {
var $scope;
beforeEach(function createChildScopeForTheTest() {
$scope = $rootScope.$new();
});
afterEach(function disposeOfCreatedChildScope() {
$scope.$destroy();
});
it('expects fromDate and toDate to be defined', function($controller) {
vm = $controller('EntryAddController', { $scope: $scope });
expect(vm.fromDay).toBeDefined();
});
});
});
});
})();
那么,为什么当我从 it
块中的函数参数中删除 $controller
时,它会抛出此错误?
Error: [$injector:unpr] Unknown provider: dogListProvider <- dogList <- EntryAddController
这两个版本的 it
块有什么区别?
it('expects fromDate and toDate to be defined', function($controller) {
vm = $controller('EntryAddController', { $scope: $scope });
expect(vm.fromDay).toBeDefined();
});
对比
it('expects fromDay to be defined', function() {
vm = $controller('EntryAddController', { $scope: $scope });
expect(vm.fromDay).toBeDefined();
});
您可以在 it()
语句中为函数包含的唯一参数是 done
(Jasmine Asynchronous Support)
it("should support async execution of test preparation and expectations", function(done) {
value++;
expect(value).toBeGreaterThan(0);
done();
});
您在函数中调用它 $controller
的事实无关紧要,它实际上是 done
函数而不是您的控制器。
您应该从函数中删除该参数并解决您的 Unknown Provider 错误。
这里有点测试新手,我正在尝试为控制器编写一些 Jasmine/Karma 测试。我得到了一个示例测试来使用和构建,在这里,$controller
被传递到 it
块的函数的参数中。这会导致测试顺利通过。
我已经成功地为这个应用程序的其他控制器编写了测试,但我还没有看到 $controller
在那里通过。所以我试着按照我所知道的去做,但是当我删除 $controller
时,测试失败了,因为它正在寻找依赖项(我应该通过 beforeEach
中的 $provide.value
提供依赖项)。 ..?)。
这是控制器代码 -
(function() {
'use strict';
angular
.module('myApp')
.controller('EntryAddController', EntryAddController);
function EntryAddController($scope, $state, moment, $sanitize, dogList, catList, months, frequencies, EntryFactory, $modal, toastr, $log) {
var vm = this;
var now = moment();
var tomorrow = now.add(1, 'days').toDate();
var to = now.subtract(1, 'days').add(12, 'months').toDate();
vm.fromDay = tomorrow;
vm.minDay = tomorrow;
start();
function start() {
//some logic here
}
})();
这是我得到的示例代码(注意 it
块)-
(function() {
'use strict';
describe('Entry Add Controller', function(){
describe('EntryAddController', function() {
var vm, $rootScope, $controller;
beforeEach(module('myApp'));
beforeEach(function() {
inject(function(_$rootScope_, _$controller_) {
$rootScope = _$rootScope_;
$controller = _$controller_;
});
});
describe('EntryAddController', function() {
var $scope;
beforeEach(function createChildScopeForTheTest() {
$scope = $rootScope.$new();
});
afterEach(function disposeOfCreatedChildScope() {
$scope.$destroy();
});
it('expects fromDate and toDate to be defined', function($controller) {
vm = $controller('EntryAddController', { $scope: $scope });
expect(vm.fromDay).toBeDefined();
});
});
});
});
})();
那么,为什么当我从 it
块中的函数参数中删除 $controller
时,它会抛出此错误?
Error: [$injector:unpr] Unknown provider: dogListProvider <- dogList <- EntryAddController
这两个版本的 it
块有什么区别?
it('expects fromDate and toDate to be defined', function($controller) {
vm = $controller('EntryAddController', { $scope: $scope });
expect(vm.fromDay).toBeDefined();
});
对比
it('expects fromDay to be defined', function() {
vm = $controller('EntryAddController', { $scope: $scope });
expect(vm.fromDay).toBeDefined();
});
您可以在 it()
语句中为函数包含的唯一参数是 done
(Jasmine Asynchronous Support)
it("should support async execution of test preparation and expectations", function(done) {
value++;
expect(value).toBeGreaterThan(0);
done();
});
您在函数中调用它 $controller
的事实无关紧要,它实际上是 done
函数而不是您的控制器。
您应该从函数中删除该参数并解决您的 Unknown Provider 错误。