简体   繁体   English

获取AngulaJS + Angular AMD + RequireJS与Karma和Jasmine配合使用时出错

[英]Error In Getting AngulaJS + Angular AMD + RequireJS to Work with Karma and Jasmine

I ma trying to add Karma & Jasmine+Require Js based Unit testing support for an AngularJS +Angular AMD & RequireJS Application that I have created. 我想为我创建的AngularJS + Angular AMD和RequireJS应用程序添加基于Karma和Jasmine + Require Js的单元测试支持。 I have been wrecking my brain around this for two days now but I am still nowhere close to sealing the deal. 我已经为此动了两天的脑筋,但是我仍然离达成交易还差得很远。

I keep getting the error : 我不断收到错误:

INFO [karma]: Karma v0.12.21 server started at http://localhost:9876/
INFO [launcher]: Starting browser Chrome
INFO [Chrome 36.0.1985 (Mac OS X 10.9.4)]: Connected on socket 8oFHaa2hqJPs0ecgIXCa with id 31963369
Chrome 36.0.1985 (Mac OS X 10.9.4) ERROR: 'There is no timestamp for     ../www/scripts/specs/UserControllerTest.js!'

WARN [web-server]: 404: /www/scripts/specs/UserControllerTest.js
Chrome 36.0.1985 (Mac OS X 10.9.4) ERROR
  Uncaught Error: Script error for: specs/UserControllerTest
  http://requirejs.org/docs/errors.html#scripterror
  at /usr/local/lib/node_modules/requirejs/require.js:141

My Code is as follows : 我的代码如下:

  1. The Karma Config file : 业力配置文件:

     // Karma configuration // Generated on Fri Aug 15 2014 20:49:40 GMT+1000 (EST) 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', 'requirejs'], // list of files / patterns to load in the browser files: [ 'test-main.js', {pattern: 'specs/*.js', included: true} ], // list of files to exclude exclude: [ ], // 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 }); }; 
  2. My test-main.js file. 我的test-main.js文件。

      var allTestFiles = []; var TEST_REGEXP = /(spec|test)\\.js$/i; var pathToModule = function(path) { return path.replace(/^\\/base\\//, '').replace(/\\.js$/, ''); }; Object.keys(window.__karma__.files).forEach(function(file) { if (TEST_REGEXP.test(file)) { // Normalize paths to RequireJS module names. allTestFiles.push(pathToModule(file)); } }); require.config({ // Karma serves files under /base, which is the basePath from your config file baseUrl: '../www/scripts', // alias libraries paths paths: { 'angular': '../libs/angular', 'angular-route': '../libs/angular-route', 'angular-animate':'../libs/angular-animate', 'angular-mocks':'../libs/angular-mocks', 'angularAMD': '../libs/angularAMD.min', 'Framework7':'../libs/framework7', 'UserController':'controller/UserCtrl', 'WebCallManager':'services/WebCallManager' }, // Add angular modules that does not support AMD out of the box, put it in a shim shim: { 'angularAMD': ['angular'], 'angular-route': ['angular'], 'angular-animate':['angular'], 'angular-mocks':['angular'], 'Framework7':{exports: 'Framework7'} }, //kick start application //deps: ['app'], // dynamically load all test files deps: allTestFiles, // we have to kickoff jasmine, as it is asynchronous callback: window.__karma__.start }); 
  3. And My Unit Test is : 我的单元测试是:

      describe('UserController', function () { var scope,controller; //mock Application to allow us to inject our own dependencies beforeEach(angular.mock.module('app')); //mock the controller for the same reason and include $rootScope and $controller beforeEach(angular.mock.inject(function($rootScope, $controller) { //create an empty scope scope = $rootScope.$new(); //declare the controller and inject our empty scope $controller('UserController', {$scope: scope}); })); it('checks the controller name', function () { expect(scope.name).toBe('Superhero'); }); }); 

I have uploaded all my code of my project to link here . 我已经上传了我项目的所有代码以链接到此处 Anyone who can help me with this is highly appreciated. 能够为我提供帮助的任何人都将受到高度赞赏。 I think In am at the end of my tether with this. 我认为In在此结束。

marcoseu is right, the There is no timestamp for... error means karma cant find the the file, but there is more. marcoseu是正确的, There is no timestamp for...错误意味着karma无法找到该文件,但还有更多。

I'd recommend making karma's base path your project root. 我建议将业力的基本路径作为项目的根目录。 This avoids Karma making the file paths absolute instead of relative which keeps things simpler and avoids path reference problems (at least on my windows OS). 这避免了Karma将文件路径设置为绝对路径而不是相对路径,这使事情变得更简单,并且避免了路径引用问题(至少在Windows OS中如此)。

Your test should be a require module (ie using define) so it can be sure the objects it requires are fully loaded. 您的测试应该是一个require模块(即,使用define),以便可以确保它所需要的对象已完全加载。 See the example test at http://karma-runner.github.io/0.12/plus/requirejs.html 请参阅http://karma-runner.github.io/0.12/plus/requirejs.html上的示例测试

karma.config.js basePath: "../", files: [ 'test/test-main.js', {pattern: 'test/specs/*.js', included: false}, {pattern: 'www/**/*.js', included: false}, ], karma.config.js basePath: "../", files: [ 'test/test-main.js', {pattern: 'test/specs/*.js', included: false}, {pattern: 'www/**/*.js', included: false}, ],

Now your files are all served by Karma under /base. 现在,您的文件都由Karma在/ base下提供。

test-main.js require.config({ baseUrl: "/base/www/scripts", test-main.js require.config({ baseUrl: "/base/www/scripts",

Debugging 调试

But most importantly you can debug all of this. 但最重要的是,您可以调试所有这些。 Run Karma, switch to the Karma created chrome instance, click the debug button, open the chrome developer tools. 运行Karma,切换到Karma创建的chrome实例,点击调试按钮,打开chrome开发人员工具。 Check the console, and your source files. 检查控制台和您的源文件。 Especially the source of debug.html, as at the bottom it has the definition of all your karma served files. 特别是debug.html的源代码,如底部所示,它定义了您所有业力提供的文件。

You can also set breakpoints and then refresh the page to watch the tests being executed. 您还可以设置断点,然后刷新页面以查看正在执行的测试。 You will be able to see for yourself why you are getting test errors. 您将可以自己查看为什么会出现测试错误。 Win. 赢得。

The error There is no timestamp for... means that karma is unable to access the file in question. 错误“ There is no timestamp for...表示karma无法访问有问题的文件。 You need to define the www directory so that is is accessible by karma. 您需要定义www目录,以便业力可以访问该目录。 Try the following: 请尝试以下操作:

karma.config.js files: [ 'test-main.js', {pattern: './specs/*.js', included: true}, {pattern: '../../www/**/*.js', included: false} ], karma.config.js files: [ 'test-main.js', {pattern: './specs/*.js', included: true}, {pattern: '../../www/**/*.js', included: false} ],

Take a look at karma.conf in the angularAMD project and the Karma documentation for files . 看看karma.confangularAMD项目和噶文档文件

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

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