Visual Studio中的角度单元测试失败

时间:2016-02-03 08:49:09

标签: angularjs unit-testing visual-studio-2015 jasmine chutzpah

安装了必要的软件包(Chutzpah,Jasmine和Karma)之后,我做了一个简单的Angular测试来尝试测试(在MVC项目中支持),我在测试时遇到了麻烦。

当我从命令行在Karma中运行测试时,它们会通过。当我在Visual Studio中通过ReSharper / Chutzpah运行它们时,它们会失败。我的测试正在运行,但我无法理解错误。

MainCtrl.js:

var myModule = angular.module("MyApp", []);
  myModule.controller("MainCtrl", ["$scope", function ($scope) {
    $scope.addNum = function(x, y) {
       return x + y;
    }

    $scope.fullName = function (x, y) {
        return x + " " + y;
    }

    $scope.age = 29;
}]);

MainCtrlSpec.js:

/// <reference path="../../AngularTesting/Scripts/_references.js" />

describe("Controller: MainCtrl", function () {
    beforeEach(module("MyApp"));
    var MainCtrl, scope;
    beforeEach(inject(function ($controller) {
        scope = {};
        MainCtrl = $controller("MainCtrl", {
            $scope: scope
        });
    }));
        it("should have scope defined", function () {
        expect(scope).toBeDefined();
    });
    it("should add 2 numbers", function () {
        expect(scope.addNum(5, 4)).toBe(9);
    });
    it("should have a name", function() {
        expect(scope.fullName("Steve", "Jackson")).toBe("Steve Jackson");
    });
    it("should have an age of 29", function () {
        expect(scope.age).toBe(29);
    });
});

Karma.conf.js:

// Karma configuration
// Generated on Tue Jan 26 2016 13:05:20 GMT-0800 (Pacific Standard Time)

module.exports = function(config) {
  config.set({
    basePath: '',

    frameworks: ['jasmine'],

    files: [
      'AngularTesting/Scripts/angular.js',
      'AngularTesting/Scripts/angular-mocks.js',
      'AngularTesting/ng-scripts/**.js',
      'AngularTesting.Tests/ng-tests/**.js'
    ],

    exclude: [
    ],

    preprocessors: {
    },

    reporters: ['progress'],

    port: 9876,

    colors: true,

    logLevel: config.LOG_INFO,

    autoWatch: true,

    browsers: ['Chrome'],

    singleRun: false,

    concurrency: Infinity
  })
}

当我在业力中进行测试时,全部4次通过。在VS Test Explorer中,它启动了Jasmine调试器,但所有4个测试都失败了。我收到以下错误:

Error: ReferenceError: Can't find variable: angular (on line 1 of MainCtrl.js)
Error: Error: Bootstrap requires jQuery
Error: TypeError: undefined is not an object (evaluating '$.extend') 
Error: TypeError: undefined is not an object (evaluating '$.validator')

同时收到警告:

Log Message: WARNING: Tried to load angular more than once.

欢迎任何有关这些错误或警告的帮助,谢谢。

1 个答案:

答案 0 :(得分:1)

您缺少chutzpah.json文件。 Chutzpah没有运行业力配置文件。只需在您的业力旁边创建一个chutzpah.json文件,然后按照http://github.com/mmanela/chutzpah

上的文档进行操作

例如:

{
  "Framework": "jasmine", 
  "References": [
    { "Path": "angular.min.js" },
    { "Path": "angular-mocks.js" },
    { "Path": "src", "Includes":["*.js"] }
  ],
  "Tests": [{ "Includes":  ["*tests.js"] }]
}