Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs Visual Studio中的角度单元测试失败_Angularjs_Unit Testing_Visual Studio 2015_Jasmine_Chutzpah - Fatal编程技术网

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

Angularjs Visual Studio中的角度单元测试失败,angularjs,unit-testing,visual-studio-2015,jasmine,chutzpah,Angularjs,Unit Testing,Visual Studio 2015,Jasmine,Chutzpah,在安装了必要的软件包(Chutzpah、Jasmine和Karma)之后,我做了一个简单的角度测试来尝试测试(在MVC项目中得到了支持),我在测试中遇到了问题 当我从命令行运行Karma中的测试时,它们通过了。当我在VisualStudio中通过ReSharper/Chutzpah运行它们时,它们失败了。我的测试正在运行,但我无法理解错误 MainCtrl.js: var myModule = angular.module("MyApp", []); myModule.controller(

在安装了必要的软件包(Chutzpah、Jasmine和Karma)之后,我做了一个简单的角度测试来尝试测试(在MVC项目中得到了支持),我在测试中遇到了问题

当我从命令行运行Karma中的测试时,它们通过了。当我在VisualStudio中通过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中运行测试时,所有4个都通过了。在VS测试资源管理器中,它启动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.

欢迎对这些错误或警告提供任何帮助,谢谢。

您缺少一个chutzpah.json文件。Chutzpah不会从karma配置文件中运行。只需在karma one旁边创建一个chutzpah.json文件,并按照

例如:

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

也许值得发布你的因果报应。conf@sdfacre添加了它,感谢您的帮助。请确保您在chutzpah.json?@sdfacre的引用部分添加了相同的js依赖项。我在任何地方都找不到chutzpah.json文件。你知道它通常在哪里吗?我将chutzpah重新安装到项目中,以确保我仍然拥有它。也许可以先读一下。如果它没有使用karma.conf文件,那么我还可以使用用于karma的ngHtml2JsPreprocessor插件来测试我的指令吗?Chutzpah!=因果报应。karma和karma的配置插件不能与Chutzpah一起使用。如果你想支持一些东西,可以在Chutzpah的github页面上提交一个isuse,或者发送一个PR
{
  "Framework": "jasmine", 
  "References": [
    { "Path": "angular.min.js" },
    { "Path": "angular-mocks.js" },
    { "Path": "src", "Includes":["*.js"] }
  ],
  "Tests": [{ "Includes":  ["*tests.js"] }]
}