无法使用Karma / Jasmine将工厂注入测试套件

时间:2016-12-21 22:34:14

标签: angularjs django karma-jasmine

我有一个带有Django后端的Angular应用程序。我刚刚开始使用Karma和Jasmine单元测试。我无法将我的工厂注入我的测试套件。我正在制作任何建议或明显的错误吗?

以下是我的文件......

karma.conf.js文件

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

basePath: '',

frameworks: ['jasmine'],

files: [
    './node_modules/angular/angular.js',                             
    './node_modules/angular-ui-router/release/angular-ui-router.js', 
    './node_modules/angular-mocks/angular-mocks.js', 
    './fake/path/fake/js/directory/src/modules.js',
    './fake/path/fake/js/directory/src/factories/projectinfostorage.js',
    './fake/path/fake/js/directory/src/factories/projectinfostorage.spec.js',
],

exclude: [
],

preprocessors: {
},

reporters: ['progress'],

port: 9876,

colors: true,

logLevel: config.LOG_INFO,

autoWatch: true,

browsers: ['Chrome'],

singleRun: false,

concurrency: Infinity
})
}

Angular模块......

var app = angular.module("tablebrowser", ["ngRoute"]);

厂...

app.factory('projectInfoStorage', function(){
    var factory = {};

    return factory;
})

工厂单位测试...

describe('projectInfoStorage factory', function() {
      var projectInfoStorage;

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

      beforeEach(inject(function(_projectInfoStorage_) {
          projectInfoStorage = _projectInfoStorage_;
      }));

      console.log(projectInfoStorage)
  });

Karma在命令行中向我显示了这个...

Chrome 55.0.2883 (Mac OS X 10.10.5) LOG: undefined

Chrome 55.0.2883 (Mac OS X 10.10.5): Executed 0 of 0 SUCCEChrome 55.0.2883     (Mac OS X 10.10.5): Executed 0 of 0 ERROR (0 secs / 0 secs)

1 个答案:

答案 0 :(得分:1)

您的测试不起作用的原因是因为您还没有真正测试过任何东西。在该describe函数中,您需要将测试实际放入it函数中。如下所示:

describe('projectInfoStorage factory', function() {
    var projectInfoStorage;

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

    beforeEach(inject(function(_projectInfoStorage_) {
        projectInfoStorage = _projectInfoStorage_;
    }));

    it('will test factory', function() {
        expect(true).toBeTruthy();
    }
});

您需要使用这些来构建测试以测试某些内容。