【发布时间】:2014-10-01 04:44:36
【问题描述】:
请参见此处,例如:http://www.johnpapa.net/angularjss-controller-as-and-the-vm-variable/
正如标题所暗示的,我正在按照本教程 [http://tech.pro/tutorial/1473/getting-started-with-angularjs-unit-testing] 设置单元测试,一切都很好,除了 我似乎无法访问 vm 变量作为我的$scope.
dashboard.js
var controllerId = 'dashboard';
angular.module('app')
.controller(controllerId, ['common', 'datacontext', dashboard]);
function dashboard(common, datacontext) {
var getLogFn = common.logger.getLogFn;
var log = getLogFn(controllerId);
var vm = this;
vm.title = 'Dashboard';
dashboard.Spec.js
describe("app module", function() {
beforeEach(module("app"));
describe("dashboard", function() {
var scope,
controller;
beforeEach(inject(function($rootScope, $controller) {
scope = $rootScope.$new();
controller = $controller;
}));
it("should assign Dashboard as title", function() {
controller("dashboard", {
$scope: scope
});
expect(scope.title).toBe("Dashboard");
});
});
});
我已经尝试过:当我直接在控制器依赖项中命名“$scope”并将“title”属性设置为它时,它可以工作(测试通过)。但是,我想保持模式不变。
我也试过直接在依赖项中传入 $scope 并将控制器参数命名为“vm”...
Karmas 测试失败消息是:Expected undefined to be 'Dashboard'
感谢任何帮助!
【问题讨论】:
标签: angularjs angularjs-scope jasmine karma-runner hottowel