【问题标题】:NodeJS, Angular Seed, Gulp, Jasmine , Karma issueNodeJS, Angular Seed, Gulp, Jasmine , Karma 问题
【发布时间】:2015-10-19 12:10:33
【问题描述】:

设置 Angular 种子,一切正常,我什至可以启动 jasmine 浏览器,但由于某种原因我的规范不起作用,所以我添加了一个 karma.conf 文件,它似乎破坏了 jasmine 浏览器。我是使用 Jasmine 的新手,所以我很茫然,关于如何最好地做到这一点有很多意见,但无论我尝试什么,我似乎都无法让它工作,我在这里添加了 repo 链接对于代码。希望有人能看到我做错了什么。

这个种子需要 NodeJS

下载git,放到文件夹里 进入文件夹,进入根目录下的config文件夹 运行 npm 安装 然后运行 ​​gulp,它会自动在浏览器中启动 在控制台中你会看到这个错误

[14:52:17] 'jasmineWeb' errored after 2.27 ms
[14:52:17] TypeError: object is not a function
    at Gulp.<anonymous> (/webdev/E20/config/gulpfile.js:137:11)
     at module.exports (/webdev/E20/config/node_modules/gulp/node_modules/orchestrator/lib/runTask.js:34:7)
at Gulp.Orchestrator._runTask (/webdev/E20/config/node_modules/gulp/node_modules/orchestrator/index.js:273:3)
at Gulp.Orchestrator._runStep (/webdev/E20/config/node_modules/gulp/node_modules/orchestrator/index.js:214:10)
at /webdev/E20/config/node_modules/gulp/node_modules/orchestrator/index.js:279:18
at finish (/webdev/E20/config/node_modules/gulp/node_modules/orchestrator/lib/runTask.js:21:8)
at module.exports (/webdev/E20/config/node_modules/gulp/node_modules/orchestrator/lib/runTask.js:60:3)
at Gulp.Orchestrator._runTask (/webdev/E20/config/node_modules/gulp/node_modules/orchestrator/index.js:273:3)
at Gulp.Orchestrator._runStep (/webdev/E20/config/node_modules/gulp/node_modules/orchestrator/index.js:214:10)
at /webdev/E20/config/node_modules/gulp/node_modules/orchestrator/index.js:279:18

这是我用来启动 Jasmine 的任务

gulp.task('jasmineWeb', function() {
   var filesForTest = ['../src/**/*.js', '../src/**/*_spec.js'] 
  return gulp.src(filesForTest)
     .pipe(karma({configFile: 'karma.conf.js',action: 'run'}))
     .pipe(watch(filesForTest))
     .pipe(jasmineBrowser.specRunner())
     .pipe(jasmineBrowser.server({port: 8888}));
});

这是我的 Karma.conf 文件

// Karma configuration
// Generated on Tue Jul 28 2015 11:28:22 GMT-0400 (EDT)

 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: [
       '../src/lib/js/angular/angular.min.js',
       '../src/lib/js/angular/angular-mocks.js',
       '../src/com/**/*.js',
       '../src/com/**/*_spec.js'
     ],

     // 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', 'Firefox', 'IE', 'PhantomJS'],


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

提前表示感谢

https://bitbucket.org/baylysoft/angular-seed-project

Karma 现在正在运行,但我现在收到此错误

PhantomJS 1.9.8 (Mac OS X 0.0.0) Controller: com/modules/AboutController Sets the Page Message It should show the page message FAILED
    ReferenceError: Can't find variable: $scope
        at /webdev/E20/src/com/modules/about/aboutController_spec.js:15
Chrome 44.0.2403 (Mac OS X 10.10.4) Controller: com/modules/AboutController Sets the Page Message It should show the page message FAILED
    ReferenceError: $scope is not defined
        at Object.<anonymous> (/webdev/E20/src/com/modules/about/aboutController_spec.js:15:16)
Chrome 44.0.2403 (Mac OS X 10.10.4): Executed 1 of 1 (1 FAILED) ERROR (0.008 secs / 0.006 secs)
PhantomJS 1.9.8 (Mac OS X 0.0.0): Executed 1 of 1 (1 FAILED) ERROR (0 secs / 0.004 secs)

这是更新的规范文件

'use strict';

describe('Controller: com/modules/AboutController', function() {
  beforeEach(module(app));

  var $controller;

  beforeEach(inject(function(_$controller_){
    // The injector unwraps the underscores (_) from around the parameter names when matching
    $controller = _$controller_;
  }));

  describe('Sets the Page Message', function() {
    it('It should show the page message', function() {
        expect($scope.message).toEqual('This is the about page message from the controller');
    });

  });
});

【问题讨论】:

标签: javascript node.js angularjs gulp karma-jasmine


【解决方案1】:

在来来回回并从这里的几个人那里得到帮助之后(非常感谢大家顺便说一句)我找到了罪魁祸首,原来问题不在实际测试中,而是在 karma 配置文件中,这是具有欺骗性的问题是我正在加载的凉亭组件,我没有加载足够的 ng-mocks 不足以测试控制器,所以我添加了一些其他的并且它有效,我能够得到我所有的测试正常工作。

这是我在 karma.conf 文件部分添加的内容

'bower_components/angular/angular.js',
        'bower_components/angular-route/angular-route.js',
        'bower_components/angular-mocks/angular-mocks.js',
        'bower_components/angular-resource/angular-resource.js',
        'bower_components/angular-cookies/angular-cookies.js',
        'bower_components/angular-sanitize/angular-sanitize.js',
        '../src/com/**/*.js',
        '../src/com/**/*_spec.js'

我已经用新的工作代码更新了 bitbucket 种子,供那些想要更好示例的人使用

bitbucket.org/baylysoft/angular-seed-project

【讨论】:

    【解决方案2】:

    我像你一样运行 Jasmine、Karma 并使用 Gulp。但是,我的启动方式存在一些差异。也许这对你有帮助。

    您的 karma.config.js 看起来很相似,所以那里可能没有问题。

    但是,在我的 gulp 文件中,我只是调用它

    gulp.task( 'karma', function( done ){
      karma.start({
        configFile: __dirname + '/karma.conf.js'
      }, done );
    });
    
    gulp karma
    

    如果您还需要监视文件的更改,那么您可以运行它。

    var filesForTest = [ '../src/**/*.js' ];
    
    gulp.task( 'watchOnSave', function(){
      gulp.watch( filesForTest, [ 'karma' ]);
    }
    
    gulp watchOnSave
    

    karma.config.js 文件将为您执行浏览器,并运行您在 karma.config.js 文件中定义的规范。

      '../src/com/**/*_spec.js' // just make sure all your JS files are in your karma **files** property.
    

    让我知道这是否可行,否则我们将提供解决方案。

    更新

    在您的应用 (app.js) 中,您有这个

    var appName = 'EuropaSeed';
    
    var globalApp = angular.module(appName,['ngRoute','ngCookies']);
    var app = angular.module(appName);
    

    在你的测试中你有

    beforeEach(module('EuroSeed'));
    

    您的问题之一是您有两个名为 EuroSeed 的模块

    app and globalApp
    

    因此,在您的测试中,请像在中那样调用您需要的两个模块中的哪一个

    beforeEach(module(app));
    

    与控制器中的内容相匹配

    app.controller('AboutController',...
    

    我正在使用它来运行规范,该规范现在正在通过,但没有使用 except,因为要让它通过还有另一个问题。

    gulp 文件为

    gulp.task('karma', function (done) {
      karma.server.start({ // had to add .server to karma
        configFile: __dirname + '/karma.conf.js'
      }, done);
    });
    


    它可能是规范,所以我想我会在这里发布规范和控制器,它们非常简单。

    现在“app”已在 app.js 文件中设置,因此应用程序本身可以工作

    关于控制器

    //This is the about function that is used in the About Module. 
        'use strict';
         app.controller('AboutController',// jshint ignore:line
            ['$scope',
            function ($scope) {
               $scope.message = 'This is the about page message from the controller';
            }]);
    

    关于控制器测试

    'use strict';
    
    describe('Controller: com/modules/AboutController', function() {
      beforeEach(module('EuropaSeed'));
    
      var $controller;
    
      beforeEach(inject(function(_$controller_){
        // The injector unwraps the underscores (_) from around the parameter     names when matching
        $controller = _$controller_;
      }));
    
      describe('Sets the Page Message', function() {
        it('It should show the page message', function() {
            expect($scope.message).toEqual('This is the about page message from the controller');
        });
    
      });
    });
    

    我取消了正在运行的测试,但我仍然在 jasmineWeb 上遇到相同的错误,因此可能是配置问题。

    Europas-MacBook-Pro:config ray$ gulp jasmineWeb
    [08:29:33] Using gulpfile /webdev/E20/config/gulpfile.js
    [08:29:33] Starting 'jasmineWeb'...
    [08:29:33] 'jasmineWeb' errored after 12 ms
    [08:29:33] TypeError: object is not a function
        at Gulp.<anonymous> (/webdev/E20/config/gulpfile.js:137:11)
        at module.exports (/webdev/E20/config/node_modules/gulp/node_modules/orchestrator/lib/runTask.js:34:7)
        at Gulp.Orchestrator._runTask (/webdev/E20/config/node_modules/gulp/node_modules/orchestrator/index.js:273:3)
        at Gulp.Orchestrator._runStep (/webdev/E20/config/node_modules/gulp/node_modules/orchestrator/index.js:214:10)
        at Gulp.Orchestrator.start (/webdev/E20/config/node_modules/gulp/node_modules/orchestrator/index.js:134:8)
        at /usr/local/lib/node_modules/gulp/bin/gulp.js:129:20
        at process._tickCallback (node.js:355:11)
        at Function.Module.runMain (module.js:503:11)
        at startup (node.js:129:16)
        at node.js:814:3
    Europas-MacBook-Pro:config ray$ 
    

    我采纳了您的建议并将 karma 任务添加到 gulp 文件中,它给了我相同的结果,所以我肯定在某个地方搞砸了配置。

    Europas-MacBook-Pro:config ray$ gulp karma
    [08:31:46] Using gulpfile /webdev/E20/config/gulpfile.js
    [08:31:46] Starting 'karma'...
    [08:31:46] 'karma' errored after 145 μs
    [08:31:46] TypeError: undefined is not a function
        at Gulp.<anonymous> (/webdev/E20/config/gulpfile.js:144:9)
        at module.exports (/webdev/E20/config/node_modules/gulp/node_modules/orchestrator/lib/runTask.js:34:7)
        at Gulp.Orchestrator._runTask (/webdev/E20/config/node_modules/gulp/node_modules/orchestrator/index.js:273:3)
        at Gulp.Orchestrator._runStep (/webdev/E20/config/node_modules/gulp/node_modules/orchestrator/index.js:214:10)
        at Gulp.Orchestrator.start (/webdev/E20/config/node_modules/gulp/node_modules/orchestrator/index.js:134:8)
        at /usr/local/lib/node_modules/gulp/bin/gulp.js:129:20
        at process._tickCallback (node.js:355:11)
        at Function.Module.runMain (module.js:503:11)
        at startup (node.js:129:16)
        at node.js:814:3
    

    这是我正在运行的更新规范

    'use strict';
    
    describe('Controller: com/modules/AboutController', function() {
    
        beforeEach(module(app));
    
        var AboutController,
            scope;
    
            beforeEach(inject(function ($rootScope, $controller) {
                scope = $rootScope.$new();
                AboutController = $controller('AboutController', {
                    "$scope": scope
                });
            }));
        it('It should show the page message', function () {
            // you might also need to do this
            AboutController();
            expect(scope.greeting).toEqual("This is the about page message from the controller");
                                           });
    
        });
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-18
    • 2015-12-03
    • 1970-01-01
    • 2015-12-07
    • 2015-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多