角度单元测试中对其他服务的附加依赖性

时间:2016-07-14 17:47:07

标签: angularjs unit-testing

describe('Controller:explorerCtrl', function() {
'use strict';
var explorerCtrl;
var explorerService;
var listService;
var scope;
var $httpBackend;
var aggregatedPods_;
var $state;
var $mdDialog;
var StateService;
var spy;

beforeEach(module(
'ui.router', explorer, kpi,
list));

 beforeEach(inject(function(
_$controller_, _$httpBackend_, _$rootScope_, _explorerService_, _$state_,
_listService_) {
scope = _$rootScope_.$new();
listService = _listService_;
aggregatedPods_ = {};
explorerService = _explorerService_;
spy = jasmine.createSpy();
StateService = {go: spy};
$httpBackend = _$httpBackend_;
$state = _$state_;
explorerCtrl =
_$controller_('explorerCtrl', {$scope: scope, $state: StateService});


}));
});

您好, 我正在尝试为单独的模块编写角度测试用例。不幸的是,上面的列表模块打破了我的测试用例。我有共同服务listServices($mdDialog)并直接在我的列表控制器(列表模块)中使用。当我尝试运行角度单位测试时,我得到一个未知错误$provider $mdDialogProvider <- $mdDialog <- listService。在这种情况下我该怎么办?

我已经尝试将$mdDialog注入$inject函数。还是有同样的错误。请纠正我在哪里做错了。

 beforeEach(inject(function(
  _$controller_, _$httpBackend_, _$rootScope_, _explorerService_, _$state_,
  _listService_ _$mdDialog_);

1 个答案:

答案 0 :(得分:4)

我认为您需要使用 $ provide 注入$ mdDialog,并且在您的控制器中需要像这样注入 $ mdDialog

beforeEach(function() {
    module(function(_$provide_) {
        _$provide_.service('$mdDialog', function() {});
    });
});

beforeEach(inject(function(
    _$controller_, 
    _$httpBackend_, 
    _$rootScope_, 
    _explorerService_, 
    _$state_, 
    _listService_,
    _$mdDialog_) {

     // ...
}));