繁体   English   中英

使用Jasmine编写单元测试,需要在Karma中使用JS才能获得Angular JS?

[英]Writing Unit Testing With Jasmine , Require JS in Karma for Angular JS?

我已经完成了使用Angular JS的图书馆管理应用程序,而Mongo Lab将充当数据库的一部分,我面临的问题是在板条箱测试用例中需要JS依赖

Error: [$injector:modulerr] Failed to instantiate module lmaApp due to:
Error: [$injector:nomod] Module 'lmaApp' is not available! 

我面临的上述错误,请帮助我摆脱困境。

我的代码:

Controller.js

define(
    ['modules'],
    function(lmaApp) {
        lmaApp.controller('gridControl',['$scope' , '$http' , 'internal' , 'commonValues', function($scope , $http , internalFn , commonValues){
            jQuery('ul.navbar-nav li').removeClass('active');
            jQuery('ul.navbar-nav li:nth-child(1)').addClass('active');
            $('.loaderImg').removeClass('no-loading');
            var lmaTableData = internalFn.getTableData(commonValues.mongoAPIurl,commonValues.bookTableName,'');
                    var promise = lmaTableData
                    .success(function(tbData) {
                        if(tbData.length > 0){
                            $scope.nobook=0;
                            $scope.books =tbData;
                        }
                        else{
                           $scope.nobook =1; 
                        }
                    }).then(function (response) {$('.loaderImg').addClass('no-loading');});
        }]);
});

modules.js

define(
    ['app_config','angularRoute'],
    function() {
        var lmaApp =angular.module('lmaApp',['ngRoute'])
        return lmaApp;
    });

app_config.js

define(
    ['angular','angularRoute','modules'],
    function() {

            angular.element(document).ready(function() {
                angular.bootstrap(document, ['lmaApp'], {});
            });

    });

我的规格文件

controllerSpec.js

define(['angular', 'angular-mocks'], function() {
    describe('gridControl', function(){

      beforeEach(module('lmaApp'));

      it('should get the book table Datas', inject(function($controller) {
        var scope = {},
            ctrl = $controller('gridControl', {$scope:scope});

        expect(scope.phones.length).not.toEqual(0);
      }));

    });
});

在这里,我对require js产生了疑问,就像我也不得不提到modules.js作为Spec文件中的依赖项一样。

您的controller-spec.js错误,您需要调用它的依赖项,即module.jsapp_config.jsController.js也是'angularRoute' 另外,使您的应用程序能够增强性能。

这样尝试

define(['angular', 'angular-mocks','modules', 'app_config', 'Controller', 'angularRoute'], function() { ... }

不过,我仍然无法确保它会为您工作。 因为如果您的任何配置错误。 它将被打破。 在单元测试中将requireJS与AngularJS一起使用在配置中非常棘手/复杂。

您应该阅读有关requireJS配置的更多信息,并使用shim定义脚本的依赖项。 当您的应用程序变大时,它将变得更加清晰和易于处理。 例如,您可以在配置文件中执行以下操作:

shim: {
    'modules': ['angular', 'app_config', 'angular-mocks'],
    'app_config': ['angular', 'angularRoute', 'modules'],
    'Controller': ['modules']
}

和您的controller-spec.js将像这样

define(['modules', 'app_config', 'Controller'], function(){ ... });

同样,在准备好的元素上进行Bootstraping角度测试也不是一个好主意。 可能会在模拟业力时引起一些冲突。 我不确定但对此感觉不对

判读您的app_config.jsmodules.js是彼此依赖的。 那么,您认为如果有需要加载的订单? 首先需要加载哪个? 因为Both都要求对方先加载,然后再由requireJS注入。

仍然认为我的解决方案不会起作用。 因为您的配置似乎错了。

暂无
暂无

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

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