mercredi 4 février 2015

Angular: Do you mock the dependencies of a controller when testing a directive?

I have a directive I am testing but it seems to want me to declare the dependencies of the controller the directive uses (ie. $modal, global ect) Is this something I need to do? If so how do you mock the dependencies for the controller when you are testing the directive? is it the same set up as mocking them for the controller?


The error I get is:



Test 'pbGoogleAnalyticsManagementList test:should call selectProfile()' failed Error: [$injector:unpr] Unknown provider: $modalProvider <- $modal <- GoogleAnalyticsManagementController



The test I am running is:



describe('pbGoogleAnalyticsManagementList', function () {
var rootScope, scope, element;
beforeEach(angular.mock.module('pb.webSites.controllers'));
beforeEach(angular.mock.module('pb.webSites.directives'));

beforeEach(inject(function ($rootScope, $compile, $templateCache) {
$templateCache.put('app/webSites/directives/GoogleAnalyticsManagementList.html', '<div></div>');
rootScope = $rootScope;
scope = $rootScope.$new();
element = angular.element('<pb-google-analytics-management-list data-pb-google-analytics-profile="webSite.googleAnalyticsProfile" data-pb-select-profile="addGoogleAnalyticsProfile(profile)">');
$compile(element)(scope);
scope.$digest();
}));

describe('test', function () {
it('should call selectProfile()', function () {
var elementScope = element.isolateScope();

spyOn(elementScope, 'selectProfile');
var data = {
account: 'account',
property: 'property',
profile: 'profile'
}

elementScope.profileClick(data.account, data.property, data.profile);
expect(elementScope.selectProfile).toHaveBeenCalledWith(data.account, data.property, data.profile);
});
})

Aucun commentaire:

Enregistrer un commentaire