使用时,从单元测试文件访问$范围VM" ControllerAs"从AngularJS HotTowel语法 [英] accessing $scope from unit test file when using the vm "ControllerAs" syntax from AngularJS HotTowel
问题描述
在这里看到,例如:<一href=\"http://www.johnpapa.net/angularjss-controller-as-and-the-vm-variable/\">http://www.johnpapa.net/angularjss-controller-as-and-the-vm-variable/
正如标题所暗示的,我在这个教程中的下列[<一个href=\"http://tech.pro/tutorial/1473/getting-started-with-angularjs-unit-testing]\">http://tech.pro/tutorial/1473/getting-started-with-angularjs-unit-testing] 我似乎设置单元测试和一切都很好除了一个事实无法访问VM变量的我的$范围的
As the title suggests, I'm following along on this tutorial [http://tech.pro/tutorial/1473/getting-started-with-angularjs-unit-testing] to setup unit testing and all is fine EXCEPT for the fact I can't seem to access the vm variable as my $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");
});
});
});
我试过什么:它的工作原理(测试通过),当我的名字直接在控制器依赖'$范围,并设置标题属性吧。不过,我想保持模式不变。
What I've tried: it works (the test passes) when I name '$scope' directly in the controllers dependencies and set the "title" property to it. However, I'd like to keep the pattern as is.
我也试过直接传递$范围,相关性和命名控制器参数的虚拟机...
I've also tried passing in $scope directly in dependencies and naming the controller parameter as "vm"...
因果报应测试失败消息:预期不确定的是仪表板
Karmas failing test message is: Expected undefined to be 'Dashboard'
AP preciate任何帮助!
appreciate any help!
推荐答案
啊,现在很明显...我可以通过制定该公司在测试中创建的控制器的引用访问VM变量:
Ah, obvious now...I can access the vm variable through making a reference to the controller that's created in the test:
it("should assign Dashboard as title", function () {
var vm = controller("dashboard", { $scope: scope });
expect(vm.title).toBe("Dashboard");
});
这篇关于使用时,从单元测试文件访问$范围VM&QUOT; ControllerAs&QUOT;从AngularJS HotTowel语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!