单元测试角/离子项目

Hos*_*ser 5 javascript unit-testing angularjs ionic-framework

我有一个非常简单的控制器,看起来像这样.

timeInOut.controller('timeInOutController', function($scope, $filter, $ionicScrollDelegate){

    ... 

});
Run Code Online (Sandbox Code Playgroud)

每当我尝试为它创建一个单元测试时......

(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(){

            ...

        });
    });
})();
Run Code Online (Sandbox Code Playgroud)

我收到错误:

[$ injector:unpr]未知提供者:$ ionicScrollDelegateProvider < - $ ionicScrollDelegate

显然,在我的例子中,我并没有尝试注入$ionicScrollDelegate测试,这只是因为我已经尝试过任何方式而没有成功,并且不知道哪个尝试包含失败.

同样在我的karma.conf.js文件中,我包括ionic.bundle.jsangular-mocks.js库/文件.

我可以成功地对任何不使用任何$ ionic的东西进行单元测试,所以我知道我的测试框架设置正确,问题是注入任何离子相关的东西.

Bro*_*cco 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(){

            ...

        });
    });
})();
Run Code Online (Sandbox Code Playgroud)

你可以通过jasmine的文档找到更多关于间谍的信息