Karma测试用例在迁移到角1.4.3后不起作用

use*_*398 5 angularjs karma-jasmine

我有一个稳定的产品,使用角1.2.23.最近,我决定转向棱角1.4.3.在与所有依赖项的几个兼容性问题之后,我的应用程序工作正常,但所有单元测试用例都已经开始失败.投资后我意识到,如果我升级所有依赖项的版本但保持角度在先前版本即1.2.23, testcases工作正常.有角度1.4.3,由于某种原因,单元测试中的依赖注入失败.

以下是bower.json中更新的依赖项列表.

  "dependencies": {
"angular-cookies": "1.4.3",
"bootstrap": "3.0.3",
"angular-ui-router": "0.2.15",
"angular-gettext": "2.1.0",
"angular": "1.4.3",
"angular-ui-utils": "3.0.0",
"restangular": "1.4.0",
"angular-route": "1.4.3",
"momentjs": "2.10.6",
"angular-i18n": "1.4.3"
 }
Run Code Online (Sandbox Code Playgroud)

以下是测试文件 -

describe("Module: x.xyz", function () {
describe("Factory: xyz", function () {
    var service;

    beforeEach(function () {
        module('x.xyz');

        inject(function ($injector) {
            service = $injector.get("xyz");
        });
    });

    describe("Testing service(): ", function () {
        describe('Testing getXYZDescription(): ', function () {
            it('should return the description for the xyz event name passed if it is available', function () {
                expect(service.getXYZDescription('abc')).toBe('abc');
            });
        });
    });
});
});
Run Code Online (Sandbox Code Playgroud)

当我运行上面的测试用例时,我得到的服务是未定义的.有人可以帮忙吗?

Ton*_* Yu 2

我从 1.3 升级到 1.4 时遇到了类似的问题angular。就我而言,我忘记angular-mocks从 1.3 升级到 1.4。我怀疑在从 1.2 到 1.3 的过渡中,模拟功能被分解为一个单独的模块,尽管我似乎找不到文档来证实这一点。在这种情况下,您的原始应用程序不需要angular-mocks依赖项,但在升级时需要添加依赖项。

您应该能够通过添加"angular-mocks": "1.4.x"到依赖项列表以及配置中已安装文件的链接来解决此问题karma。为了完整起见,这里有一个最小的例子:

karma.conf.js

/*global module*/

module.exports = function (config) {
    'use strict';

    config.set({
        basePath: '.',
        frameworks: ['jasmine'],
        files: [
            'node_modules/angular/angular.js',
            'node_modules/angular-mocks/angular-mocks.js',
            '*.js'
        ],
        autoWatch: true,
        singleRun: false,
        browsers: ['Chrome']
    });
};
Run Code Online (Sandbox Code Playgroud)

package.json

{
  "name": "angular-inject-test",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "karma start karma.conf.js"
  },
  "author": "",
  "dependencies": {
    "angular": "1.4.x",
    "angular-mocks": "1.4.x",
    "karma": "^0.13.x",
    "karma-cli": "^0.1.x",
    "karma-jasmine": "^0.3.x",
    "karma-chrome-launcher": "^0.2.x"
  }
}
Run Code Online (Sandbox Code Playgroud)

test.js

/*global angular, beforeEach, describe, expect, inject, it, module*/


angular.module('x', [])
    .factory('xyz', function () {
        "use strict";
        return {
            getXYZDescription: function (value) {
                return value;
            }
        };
    });


describe("Module: x.xyz", function () {
    "use strict";

    describe("Factory: xyz", function () {
        var service;

        beforeEach(function () {
            module('x');

            inject(function ($injector) {
                service = $injector.get("xyz");
            });
        });

        it('Should echo input', function () {
            expect(service.getXYZDescription('abc')).toBe('abc');
        });
    });
});
Run Code Online (Sandbox Code Playgroud)