问题描述
我刚刚开始使用茉莉来测试AngularJS应用。
我相信我做的所有的SpecRunner进口,但我得到的错误:
类型错误:$ $范围上不是一个函数
我在我的控制器如下使用这个:
app.controller('asstCtrl',函数($范围,$ uibModal,$区间,$文件){
($范围。在$('$ includeContentLoaded',功能){
$ scope.doStuff();
});
});...
我SpecRunner测试看起来像这样:
描述('计算器',函数(){
beforeEach(模块('助理'));
它('基本',注入(函数($控制器){
VAR范围= {},
CTRL = $控制器('asstCtrl',{$范围:适用范围});
期待(scope.x).toBe(1);
}));
});
这是我进口:
- 公共/ JS /茉莉-2.4.0 / jasmine.js
- 公共/ JS /茉莉-2.4.0 /茉莉html.js
- 公共/ JS /茉莉-2.4.0 / boot.js
- 公共/ JS / jquery.min.js
- 公共/ JS / angular.js公共/ JS /角animate.min.js
- 公共/ JS /角mocks.js
- 公共/ JS / UI的引导,第三方物流企业,0.14.2.min.js
该应用程序运行正常,测试运行,如果我删除$ $范围从我的控制器部分。我必须做的事SpecRunner错了或什么的工作方式不同,当我注入控制器。谁能帮我这个?
修改
测试结束这样看:
描述('计算器',函数(){
beforeEach(模块('助理'));
VAR rootScope;
beforeEach(注(函数($ rootScope){
rootScope = $ rootScope;
}));
它('基本',注入(函数($控制器){
VAR范围= rootScope。美元的新()
CTRL = $控制器('asstCtrl',{$范围:适用范围});
期待(scope.x).toBe(1);
}));
});
您创建了一个对象,并注入,作为控制器的范畴。不会有该对象的 $关于
方法。
相反注入 $ rootScope
和使用方法 $新的
来创建范围:
VAR范围= $ rootScope。$新的()
CTRL = $控制器('asstCtrl',{$范围:适用范围});
I've just started using Jasmine for testing an AngularJS app.I believe I'm doing all the imports on the SpecRunner but I get the error:
TypeError: $scope.$on is not a function
I'm using this in my controller as follows:
app.controller('asstCtrl', function($scope, $uibModal, $interval, $document) {
$scope.$on('$includeContentLoaded', function() {
$scope.doStuff();
});
});
...
My SpecRunner test is looking like this:
describe('calculator', function(){
beforeEach(module('asst'));
it('basic', inject(function($controller) {
var scope = {},
ctrl = $controller('asstCtrl', {$scope:scope});
expect(scope.x).toBe(1);
}));
});
These are my imports:
- public/js/jasmine-2.4.0/jasmine.js
- public/js/jasmine-2.4.0/jasmine-html.js
- public/js/jasmine-2.4.0/boot.js
- public/js/jquery.min.js
- public/js/angular.js public/js/angular-animate.min.js
- public/js/angular-mocks.js
- public/js/ui-bootstrap-tpls-0.14.2.min.js
The app runs fine and the test runs if I remove the $scope.$on section from my controller. I must be doing something wrong in the SpecRunner or something works differently when I inject the controller. Can anyone help me with this?
EDIT
Test ended up looking like this:
describe('calculator', function(){
beforeEach(module('asst'));
var rootScope;
beforeEach(inject(function($rootScope) {
rootScope = $rootScope;
}));
it('basic', inject(function($controller) {
var scope = rootScope.$new(),
ctrl = $controller('asstCtrl', {$scope:scope});
expect(scope.x).toBe(1);
}));
});
You are creating an object and injecting that as the controller´s scope. There will not be a $on
method on that object.
Instead inject $rootScope
and use the method $new
to create a scope:
var scope = $rootScope.$new(),
ctrl = $controller('asstCtrl', { $scope: scope });
这篇关于$范围。在茉莉花SpecRunner不工作$的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!