好的,所以我试图第一次在Jasmine上开始Angular JS项目的单元测试,但遇到了一些障碍。
我正在关注此博客文章中有关如何在Visual Studio中进行所有设置的很好的参考。
我的测试在一个类库项目中,看起来像这样:
/// <reference path="../../../../src/tasks.web/scripts/angular-1.2.10.js" />
/// <reference path="../../../../src/tasks.web/scripts/angular-route-1.2.10.js" />
/// <reference path="../../../../src/tasks.web/scripts/app.js" />
/// <reference path="../../../../src/tasks.web/scripts/controllers.js" />
/// <reference path="../lib/angular/angular-mocks.js" />
describe("TaskListController", function () {
beforeEach(module('taskApp.controllers'));
it("has tasks defined", function() {
var $scope = {};
$controller("TaskListController", { $scope: $scope });
expect($scope.tasks).toBeDefined();
});
});
我的应用程序的其余部分看起来像这样:
app.js
var app = angular.module("taskApp", [
"ngRoute",
"appControllers"
]);
app.config(["$routeProvider",
function($routeProvider) {
$routeProvider.
when("/tasks", {
templateUrl: "partials/task-list.html",
controller: "TaskListController"
}).
otherwise({
redirectTo: "/tasks"
});
}
]);
controller.js
var appControllers = angular.module("appControllers", []);
appControllers.controller("TaskListController", function ($scope) {
$scope.tasks = {};
});
所有引用的文件都在那里(没有404)。Visual Studio很好地找到了测试并运行了它,但是我得到了ReferenceError: Can't find variable: $controller
我很高兴收到任何帮助,因为我知道我真的很努力进行这项工作,而且我知道这只是我所缺少的一些简单信息。
您需要注入$controller
。一种方法是:
it("has tasks defined", inject(function($controller) {
var $scope = {};
$controller("TaskListController", { $scope: $scope });
expect($scope.tasks).toBeDefined();
}));
另外,您还需要在 beforeEach
因此,您需要执行以下操作:
beforeEach(module('appControllers'));
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句