相关疑难解决方法(0)

在Jasmine单元测试中模拟AngularJS模块依赖项

我试图在一个模块中对控制器代码进行单元测试,该模块将其他模块作为依赖项,但是无法弄清楚如何正确地模拟它们.

我正在使用Jasmine Framework并使用Karma(Testacular)运行我的测试.

模块代码

var app = angular.module('events', ['af.widgets', 'angular-table']);

app.controller('eventsCtrl', function([dependencies]){
    $scope.events = [];
    ...
});
Run Code Online (Sandbox Code Playgroud)

规格代码

describe('events module', function(){
    var $scope,
        ctrl;

    beforeEach(function(){
        angular.mock.module('af.widgets', []);
        angular.mock.module('angular-table', []);
        module('events', ['af.widgets', 'angular-table']);
    });

    beforeEach(inject(function($rootScope, $controller){
        $scope = $rootScope.new();
        ctrl = $controller('NameCtrl', {
            $scope: $scope,
        });
    }));

    it('should have an empty events array', function(){
        expect($scope.events).toBe([]);
    })
});
Run Code Online (Sandbox Code Playgroud)

我得到的错误是Karma是"没有模块af.widgets",所以显然我并没有正确地模仿模块依赖.任何提示?

unit-testing jasmine angularjs karma-runner

71
推荐指数
2
解决办法
8万
查看次数

标签 统计

angularjs ×1

jasmine ×1

karma-runner ×1

unit-testing ×1