我有一个非常简单的控制器,看起来像这样。
timeInOut.controller('timeInOutController', function($scope, $filter, $ionicScrollDelegate){
...
});
每当我尝试为它创建一个单元测试时......
(function() {
'use strict';
var scope, controller, filter;
describe('timeInOutController', function () {
beforeEach(module('common.directives.kmDateToday'));
beforeEach(inject(function ($rootScope, $controller, $filter) {
scope = $rootScope.$new();
filter = $filter;
controller = $controller('timeInOutController', {
$scope: scope
});
}));
describe('#date setting', function(){
...
});
});
})();
我收到错误:
[$ injector:unpr]未知提供者:$ ionicScrollDelegateProvider< - $ ionicScrollDelegate
显然,在我的例子中,我并没有尝试将$ionicScrollDelegate
注入测试中,这只是因为我尝试过任何方式都没有成功,并且不知道哪个尝试包含失败
同样在我的karma.conf.js
文件中,我包含ionic.bundle.js
和angular-mocks.js
个库/文件。
我可以成功地对任何不使用任何$ ionic的东西进行单元测试,所以我知道我的测试框架设置正确,问题是注入任何离子相关的东西。
答案 0 :(得分:6)
如果您要通过角度实例化控制器,则需要传入所有参数。通过添加参数,您可以告诉角度,只要您创建其中一个控制器,我需要这些东西,因为我依赖它们。
所以我的建议是模拟这些依赖项的一些表示,并在创建控制器时注入它们。它们不一定(也不应该)是您的单元测试的实际服务。 Jasmine使您能够创建可以注入的间谍对象,以便您可以验证此单元的行为。
(function() {
'use strict';
var scope, controller, filter, ionicScrollDelegate;
describe('timeInOutController', function () {
beforeEach(module('common.directives.kmDateToday'));
beforeEach(inject(function ($rootScope, $controller, $filter) {
scope = $rootScope.$new();
filter = $filter;
// func1 and func2 are functions that will be created as spies on ionicScrollDelegate
ionicScrollDelegate = jasmine.createSpyObj('ionicScrollDelegate', ['func1', 'func2']
controller = $controller('timeInOutController', {
$scope: scope,
$filter: filter,
$ionicScrollDelegate: ionicScrollDelegate
});
}));
describe('#date setting', function(){
...
});
});
})();
找到有关间谍的更多信息
答案 1 :(得分:3)
您需要为控制器正在使用的所有依赖项创建模拟对象。
以此控制器为例:
angular.module('app.module', [])
.controller('Ctrl', function($scope, $ionicLoading) {
$ionicLoading.show();
});
您正在使用$ionicLoading
服务,因此,如果您要测试此控制器,则必须模拟该对象指定您在控制器中使用的方法:
describe('Test', function() {
// Mocks
var $scope, ionicLoadingMock;
var ctrl;
beforeEach(module('app.module'));
beforeEach(function() {
// Create $ionicLoading mock with `show` method
ionicLoadingMock = jasmine.createSpyObj('ionicLoading', ['show']);
inject(function($rootScope, $controller) {
$scope = $rootScope.$new();
ctrl = $controller('Ctrl', {
$scope: $scope,
$ionicLoading: ionicLoadingMock
});
});
});
// Your test goes here
it('should init controller for testing', function() {
expect(true).toBe(true);
});
});