Struggling to get unit testing set up in Jasmine/Karma. I have a controller with a service dependency, and that service has another service dependency. I am not organizing my modules by type (Directives, Services, etc), but rather by feature (layout, summaryView, etc).
Here's the architecture:
angular.module('myApp', ['ngResource', 'myApp.base', 'myApp.layout','myApp.common']);
angular.module('myApp.base', ['myApp.common']);
angular.module('myApp.common',[]);
angular.module('myApp.layout',['myApp.common']);
Controller:
angular.module('myApp.layout')
.controller('LayoutCtrl', ['$scope', '$rootScope', '$timeout', 'layoutService', 'urlService', 'BaseService',
function ($scope, $rootScope, $timeout, layoutService, urlService, BaseService) {
//controller code here
});
Layout Service:
angular.module('myApp.layout')
.service('layoutService', ['$http', '$resource', '$rootScope', '$location', '$route', 'errorHandlingService', 'utilService',
function ($http, $resource, $rootScope, $location, $route, errorHandlingService, utilService) {
//service code here
});
From what I understand, if I simply include beforeEach(module('myApp.layout'));
, I should have access to my controllers, services, filters, and directives in my layout module.
Instead, the following code fails:
describe('Layout Controller', function() {
var ctrl, scope, service;
beforeEach(module('myApp'));
beforeEach(module('myApp.layout'));
beforeEach(inject(function($controller, $rootScope, layoutService) {
scope = $rootScope.$new();
service = layoutService;
//Create the controller with the new scope
ctrl = $controller('LayoutCtrl', {$scope: scope, layoutService: service});
dump(scope);
}));
it('should exist', function() {
expect(ctrl).toBeDefined();
});
});
With this error:
Chrome 26.0 (Mac) Layout Controller should exist FAILED
Error: Unknown provider: layoutServiceProvider <- layoutService
at Error (<anonymous>)
at http://ajax.googleapis.com/ajax/libs/angularjs/1.0.4/angular.min.js:28:236
at Object.c [as get] (http://ajax.googleapis.com/ajax/libs/angularjs/1.0.4/angular.min.js:26:13)
at http://ajax.googleapis.com/ajax/libs/angularjs/1.0.4/angular.min.js:28:317
at c (http://ajax.googleapis.com/ajax/libs/angularjs/1.0.4/angular.min.js:26:13)
at Object.d [as invoke] (http://ajax.googleapis.com/ajax/libs/angularjs/1.0.4/angular.min.js:26:147)
at workFn (http://code.angularjs.org/1.0.4/angular-mocks.js:1754:20)
Error: Declaration Location
at window.jasmine.window.inject.angular.mock.inject (http://code.angularjs.org/1.0.4/angular-mocks.js:1740:25)
at null.<anonymous> (/Users/scottsilvi/svn/BARO/web/src/test/js/unit/myApp.layoutModule.js:6:14)
at /Users/scottsilvi/svn/BARO/web/src/test/js/unit/myApp.layoutModule.js:1:1
Expected undefined to be defined.
Error: Expected undefined to be defined.
at null.<anonymous> (/Users/scottsilvi/svn/BARO/web/src/test/js/unit/myApp.layoutModule.js:15:16)
Chrome 26.0 (Mac): Executed 10 of 10 (1 FAILED) (0.36 secs / 0.014 secs)
Thoughts?
Testing in AngularJS is achieved by using the karma framework, a framework which has been developed by Google itself. The karma framework is installed using the node package manager. The key modules which are required to be installed for basic testing are karma, karma-chrome-launcher ,karma-jasmine, and karma-cli.
AngularJS is written with testability in mind, but it still requires that you do the right thing. We tried to make the right thing easy, but if you ignore these guidelines you may end up with an untestable application.
The Angular CLI downloads and installs everything you need to test an Angular application with the Jasmine test framework.
Jasmine is a behavior-driven development framework for testing JavaScript code. It does not depend on any other JavaScript frameworks. It does not require a DOM. And it has a clean, obvious syntax so that you can easily write tests. Karma is a test runner that fits all our needs in the angular framework.
Often a Unknown Provider error comes from files not being loaded, or being loaded in the incorrect order. Check which files are present while your tests are running.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With