2015-08-14 44 views
0

我只是试图开始单元测试Angular代码,并试图使用Karma和Jasmine进行测试。我已经通过几个不同的教程进行了检查并模拟了他们的代码,但是每次我的测试中$ scope都是未定义的。我一直在这一整天工作,已经阅读了每一个相关的Stackoverflow问题(一对夫妇似乎相关但没有得到答案),不断缩小问题的范围,使事情变得简单和简单等。我正要将我的头发拉出来。如果有人能帮助我,我将非常感激!

karma.config.js:

// Karma configuration 
// Generated on Tue Aug 11 2015 11:40:35 GMT-0500 (Central Daylight Time) 

module.exports = function(config) { 
    config.set({ 

    // base path that will be used to resolve all patterns (eg. files, exclude) 
    basePath: '', 


    // frameworks to use 
    // available frameworks: https://npmjs.org/browse/keyword/karma-adapter 
    frameworks: ['jasmine'], 


    // list of files/patterns to load in the browser 
    files: [ 
     'Scripts/angular-min.js', 
     'Scripts/angular-mocks.js', 
     'Scripts/angular-route-min.js', 
     'Scripts/angular-resource.min.js', 
     'Scripts/hr-module.js', 
     'Scripts/jquery-1.8.2.js', 
     'Scripts/Employee/*.js' 

    ], 


    // list of files to exclude 
    exclude: [ 
     'Scripts/_references.js', 
     'Scripts/jquery-1.8.2.intellisense.js' 
    ], 


    // preprocess matching files before serving them to the browser 
    // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor 
    preprocessors: { 
    }, 


    // test results reporter to use 
    // possible values: 'dots', 'progress' 
    // available reporters: https://npmjs.org/browse/keyword/karma-reporter 
    reporters: ['progress'], 


    // web server port 
    port: 9876, 


    // enable/disable colors in the output (reporters and logs) 
    colors: true, 


    // level of logging 
    // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG 
    logLevel: config.LOG_INFO, 


    // enable/disable watching file and executing tests whenever any file changes 
    autoWatch: true, 


    // start these browsers 
    // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher 
    browsers: ['Chrome'], 


    // Continuous Integration mode 
    // if true, Karma captures browsers, runs the tests and exits 
    singleRun: false 
    }) 
} 

简单controller.js:

(function() { 
    'use strict'; 

    angular.module('hrModule').controller('SimpleController', ['$scope', function ($scope) {   
     $scope.greeting = "hello world";   
    }]); 


}()); 

测试简单的控制器:

'use strict'; 

describe('SimpleController', function() { 

    beforeEach(angular.mock.module('hrModule')); 

    var $controller, 
     scope, 
     SimpleController; 


    beforeEach(angular.mock.inject(function ($rootScope, _$controller_) { 
     scope = $rootScope.$new(); 
     $controller = _$controller_; 
     SimpleController = $controller('SimpleController', { $scope: scope }); 
    })); 


    it('says hello world', function() { 
     expect(scope).toBeDefined(); 
    });  

}); 

我不断收到($注射器:modulerr)错误,当我运行它,并且说“期望undefined被定义”。如果有人在这里看到任何问题,请告诉我!谢谢!

+1

当你在控制台上的modulerr链接上单击**时,它说了什么?还有你加载了脚本声明模块'angular.module('hrModule',[...])'? – PSL

+0

是的,hr-module.js文件实际上是创建hrModule的文件(我应该提到或包含代码)。并且在Windows上,我无法点击控制台中的任何内容。但是在那个错误之后有大量的编码数据,所以我假设这可能是如果我能够显示的信息,但我无法读取它。如果那里有我缺少的良好信息,我可以在另一个安装程序上尝试代码,看看我能否看到它。 – LeftOnTheMoon

+1

什么意思是“在窗户上”。我正在谈论浏览器控制台。或者只需从浏览器中复制粘贴该网址并点击即可。也可以尝试使用非缩小的angularjs开发,这会给你更详细的信息。如果您按照控制台中的错误链接,通常会导致这些错误很容易识别。 – PSL

回答

1

谢谢你,PSL在这方面与我一起工作。我真的很感激你回来继续提供提示。

但是,解决方案比这更普通。这是我从Nuget(最新版本,1.4.x)获取的angular.js文件与我从教程链接(1.3.3)下载的angular-mock.js脚本之间的不兼容性。出于某种原因,这导致了令我莫名其妙的模块化错误,但我在用相同的文件重新构建一个较小的测试项目以在浏览器中测试它时发现了它。

因此,如果其他人得到这样的错误,打开你的角库文件,并确保它们都共享相同的版本号!

+0

其实,对不起,我相信这是使用最新版本的Jasmine和旧版本的angular-mock.js的问题。无论哪种方式,抓住angular-mock.js v1.4.4应该纠正这个问题。 – LeftOnTheMoon