chhitij srivastava chhitij srivastava - 4 months ago 55
AngularJS Question

Additional dependencies on other services in angular unit test

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});


}));
});


Hi,
I am trying to write angular test cases for my separate module. Unfortunately, my list module above breaks down my test cases. I have common service
listServices($mdDialog)
and using directly in my list controller(list module). When I am trying to run my angular unit test then I got an error unknown
$provider $mdDialogProvider <- $mdDialog <- listService
.What should I do in this case ?

I already tried to inject
$mdDialog
into
$inject
function. Still got same error. Please correct me where I am doing wrong.



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

Answer

I think you need to inject the $mdDialog using $provide and in your controller you need to inject $mdDialog

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

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

like this..