简体   繁体   中英

error by injecting the control unit tests the angular

I have problems to create tests in angular using jasmine and karma. If you can help me I appreciate it. the console logs Error: [$ injector: modulerr] with a link that claims to be an error that ocore when a module fails to load due to some exception.

in karma config file:

files: [
    'bower_components/angular/angular.min.js',  
    'bower_components/angular-route/angular-route.min.js', 
    'node_modules/angular-mocks/angular-mocks.js',
    'www/lib/masks.min.js',
    'www/lib/ui-utils-mask.js',
    'www/js/app.js', 'www/js2/*.js',
    'tests/*.test.js' 
],

my test:

describe('Register', function () {
    beforeEach(angular.mock.module('app', ['onsen', 'ui.utils.masks']));
    var $controller;
    beforeEach(inject(function (_$controller_) {
        $controller = _$controller_;
    }));
    describe('bla bla bla', function () {
        it('tesye ', function () {
            var $scope = {};
            console.log("$controler: " + $controller);
        });
    });
});

If I comment the line 4 and line 5 (beforeEach), the same mistake does not happen, but the property 'controller' is undefined.

I think you do not use $controller as intended. It should only serve to load a controller you want to test like this :

var ctrl, $scope;
beforeEach(inject(function ($controller, _$rootScope_){
    $scope = _$rootScope.$new();
    ctrl = $controller('MyCtrl', {$scope: $scope});
}));
describe('bla bla bla', function () {
    it('tesye ', function () {
        console.log("ctrl" + ctrl);
        console.log("My controller scope "+$scope);
    });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM