在运行咕噜测试
在约曼(1.0RC1)脚手架角(1.0.7)的应用程序,我得到了以下错误:
While running grunt test
on a Yeoman (1.0RC1) scaffolded Angular (1.0.7) app, I'm getting the following error:
TypeError: 'undefined' is not a function (evaluating '$scope.$parent.userLoggedIn(true)')
userLoggedIn()
是父控制器内 index.js
。该函数本身运行的角度应用程序内的罚款。
userLoggedIn()
is within a parent controller index.js
. The function itself runs fine within the angular app.
不会出现在控制器我的其他 $范围。$父
布尔或字符串变量这个错误,因此它直接关系到父之内调用函数。
This error doesn't occur on my other $scope.$parent
boolean or strings variables in the controller, so it's related directly to calling functions within a parent.
我在想,我是不是使用 $范围。$父
错误的方式或者说我需要定义我的 index.js
控制器的测试,但测试噶文档零星的,所以很难知道的。
I'm thinking that I'm either using $scope.$parent
the wrong way or that I need to define my index.js
controller in the test, but Karma testing documentation is sporadic, so it's hard to know.
编辑:这里是控制器:
'use strict';
angular.module('uiApp')
.controller('LookupCtrl', ['$scope', function ($scope) {
$scope.awesomeThings = [
'HTML5 Boilerplate',
'AngularJS',
'Karma'
];
$scope.$parent.userLoggedIn(true);
}]);
下面是测试:
'use strict';
describe('Controller: LookupCtrl', function () {
// load the controller's module
beforeEach(module('uiApp'));
var LookupCtrl,
scope;
// Initialize the controller and a mock scope
beforeEach(inject(function ($controller, $rootScope) {
scope = $rootScope.$new();
LookupCtrl = $controller('LookupCtrl', {
$scope: scope
});
}));
it('should attach a list of awesomeThings to the scope', function () {
expect(scope.awesomeThings.length).toBe(3);
});
});
(是的,我知道,我只是使用默认的 awesomeThings
测试吧。我是新来的角测试)。
(Yes I know I'm just using the default awesomeThings
test for now. I'm new to Angular testing).
推荐答案
您正在给一个控制器中的 rootScope
,它不具有 $父
(这是根)。改变你的控制器code正确地调用它(使用原型链),你会被刚好路过罚款。{我:道具}
为对象
You're giving the controller a rootScope
, which doesn't have $parent
(it's the root).
Change your controller code to call it correctly (using the prototype chain) and you'll be fine by just passing {my: props}
as an object.
相关推荐
最新文章