【发布时间】:2016-05-12 08:50:15
【问题描述】:
安装了必要的包(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
})
}
当我在 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.
欢迎任何有关这些错误或警告的帮助,谢谢。
【问题讨论】:
-
可能值得发布您的 karma.conf
-
@sdfacre 已添加,感谢您的帮助
-
只要确保您在 chutzpah.json 的引用部分中添加了相同的 js 依赖项?
-
@sdfacre 我在任何地方都找不到 chutzpah.json 文件。有什么想法通常位于它的位置吗?我将 chutzpah 重新安装到项目中以确保我仍然拥有它。
-
也许先读到这个。 chutzpah.codeplex.com/…
标签: angularjs unit-testing visual-studio-2015 jasmine chutzpah