简体   繁体   English

通过注入控制单元测试角度误差

[英]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. 控制台会记录错误:[$ jector:modulerr],并带有一个链接,该链接声称是模块由于某些异常而无法加载时出现的错误。

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. 如果我注释第4行和第5行(beforeEach),则不会发生相同的错误,但是未定义属性“ controller”。

I think you do not use $controller as intended. 我认为您没有按预期使用$ controller。 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);
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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