如何使用karma / jasmine测试browserify项目

时间:2014-10-23 12:30:49

标签: jasmine karma-runner browserify karma-jasmine

我对测试的概念完全陌生,我需要一个关于如何在我的项目中做到这一点的实例:

我有一个gulp文件就像这样(不是全部,只是重要的部分)

gulp.task('bundle', function() {
    gulp.src('public/angular-app/main.js')
        .pipe(browserify({
            debug: true
        }))
        .pipe(gulp.dest('public/min-js'));           
});

这是我的main.js的一小部分:

'use strict';

    angular.module('myApp', [
        'ui.router',
        'ui.bootstrap',
        'ngSanitize',
        'ngFx',
        ...
    ], ['$interpolateProvider',
        function($interpolateProvider) {
            $interpolateProvider.startSymbol('{{');
            $interpolateProvider.endSymbol('}}');
        }
    ])

    .config(require('./config/routes'))

        .config(require('./config/authInterceptor'))
        .run(require('./config/runPhase'))
        .run(require('./config/xeditable'))



        .controller('homeController', require('./controllers/homeController'))
        .controller('modalInstanceCtrl', require('./controllers/modalInstanceCtrl'))
        .controller('modalparticipantCtrl',require('./controllers/modalParticipantCtrl'))
        .controller('generatorController',require('./controllers/generatorController'))
        .controller('navController', require('./controllers/navController'))
        .controller('signInController', require('./controllers/signInController'))
        .controller('pricingController', require('./controllers/pricingController'))
        .controller('howItWorksController',require('./controllers/howItWorks'))
        ...

现在这是我的karma配置文件:

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: [
      'public/vendor/jquery/dist/jquery.js',
      'public/vendor/angular/angular.js',
      'public/vendor/angular-mocks/angular-mocks.js',
      'public/angular-app/**/*.js',
      'test/**/*Spec.js'
    ],


    // list of files to exclude
    exclude: [
    ],

当我用业力开始运行业力时,这就是我得到的:

未捕获的引用错误:未定义require 在root / public / angular-app / main.js

所以我的问题很简单,我如何进行测试,例如,在我的homeController上...

//更新

所以我将测试文件更新为:

describe("An Angularjs test suite",function(){
    var target, rootScope;
    beforeEach(inject(function($rootScope) {
      rootScope = $rootScope;

      // Mock everything here
      spyOn(rootScope, "$on")
    }));

    beforeEach(inject(function(homeController) {
      target = homeController;
    }));

    it('should have called rootScope.$on', function(){
      expect(rootScope.$on).toHaveBeenCalled();
    });

});

和我的配置文件:

// list of files / patterns to load in the browser
files: [
  'public/vendor/jquery/dist/jquery.js',
  'public/vendor/angular/angular.js',
  'public/vendor/angular-mocks/angular-mocks.js',
  'public/min-js/main.js',
  'test/**/*Spec.js'
],


// list of files to exclude
exclude: [
],


browserify: {
        watch: true,
        debug: true
},

preprocessors: {
    'test/*': ['browserify']
},

仍然没有任何作用,首先他说'未知的提供者homeControllerProvider', 现在,如果我删除它们行:

beforeEach(inject(function(homeController) {
          target = homeController;
        }));

它仍然给我错误,预期间谍$ on会被调用,我如何解决这个问题?

1 个答案:

答案 0 :(得分:3)

在运行测试之前,您需要通知Karma运行Browserify。

您可以在您的Karma配置中添加:

    {
        browserify: {
            watch: true,
            debug: true
        },
        preprocessors: {
            'test/*': ['browserify']
        }
    }

Karma配置文件参考:http://karma-runner.github.io/0.12/config/configuration-file.html

或者看一下我使用Karma进行测试的项目之一:smild