- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我对单元测试还是很陌生,老实说,我什至想不出任何测试,但除非我有至少 1 个测试用例,否则我无法构建我的应用程序,所以我尝试制作我能用的最简单的测试用例,在 Controller 中的最小代码块上,但它似乎无法正常工作。
我认为这是我的测试用例中的错误,而不是我的 Controller 代码本身的错误,因为当我在浏览器中使用 grunt serve
查看我的应用程序时,控制台没有显示任何错误。
这是它给我的错误:
PhantomJS 2.1.1 (Linux 0.0.0) Controller: MainCtrl should attach a list of jackpot to the scope FAILED
/home/elli0t/Documents/Yeoman Projects/monopoly/app/bower_components/angular/angular.js:3746:53
forEach@[native code]
forEach@/home/elli0t/Documents/Yeoman Projects/monopoly/app/bower_components/angular/angular.js:323:18
loadModules@/home/elli0t/Documents/Yeoman Projects/monopoly/app/bower_components/angular/angular.js:3711:12
createInjector@/home/elli0t/Documents/Yeoman Projects/monopoly/app/bower_components/angular/angular.js:3651:22
workFn@/home/elli0t/Documents/Yeoman Projects/monopoly/app/bower_components/angular-mocks/angular-mocks.js:2138:60
TypeError: undefined is not an object (evaluating 'scope.jackpot') in /home/elli0t/Documents/Yeoman Projects/monopoly/test/spec/controllers/main.js (line 20)
/home/elli0t/Documents/Yeoman Projects/monopoly/test/spec/controllers/main.js:20:17
PhantomJS 2.1.1 (Linux 0.0.0): Executed 1 of 1 (1 FAILED) ERROR (0.04 secs / 0.007 secs)
这是我的测试用例:
it('should attach a list of jackpot to the scope', function () {
expect(scope.jackpot.length).toBe(2);
});
这是我尝试运行测试的代码块:
var countInJackpot = localStorageService.get('jackpot');
$scope.jackpot = countInJackpot || [
{
letter: '$',
prize: '$1,000,000 Cash',
numbers: ['$611A','$612B','$613C','$614D','$615E','$616F','$617G','$618F'],
count: [0,0,0,0,0,0,0,0]
},
{
letter: '?',
prize: '$500,000 Vacation Home',
numbers: ['?619A','?620B','?621C','?622D','?632E','?624F','?625G','?626H'],
count: [0,0,0,0,0,0,0,0]
}
];
目前,我真的只想编写 1 个简单的测试用例,所以它会让我构建应用程序。我目前正在学习单元测试,但我仍然没有准备好自己编写更复杂的测试用例。我会保存它以备后用。
如果需要,我已将文件的全部内容包含在要点中以供引用,并且如果需要,我可以包含 karma.conf.js 的内容。
最佳答案
在你的测试用例中,范围应该是 $scope?
或
您可能还没有设置测试环境以加载到您的 Controller 中。
这是我的一个 Controller 测试示例...Angular 使设置学习起来有点困难,但是一旦您理解了流程。太棒了:)
我将尝试添加尽可能多的评论来解释每篇文章...但如果您需要澄清,请告诉我。您可能正在使用 jasmine,但请记住,这是 mocha,我使用的是通过 karma.conf 加载的 Angular 模拟库。
describe('myController', function() {
var $scope,
createController;
// Runs before each test. Re-extantiating the controller we want to test.
beforeEach(inject(function($injector) {
// Get hold of a scope (i.e. the root scope)
$scope = $injector.get('$rootScope');
// The $controller service is used to create instances of controllers
var $controller = $injector.get('$controller');
createController = function() {
// Creates the controller instance of our controller.
// We are injecting $scope so we will have access to it
// after the controllers code runs
return $controller('myCtrl', {
'$scope': $scope
});
};
}));
describe('#myFunction', function() {
it('jackpot should contain two objects', function() {
expect($scope.jackpot.length).to.equal(2);
});
});
});
希望对您有所帮助。这是我用来学习的一些资源 :) 祝你好运!
关于javascript - Karma + PhantomJS TypeError : undefined is not an object (evaluating scope. 大奖),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36707003/
我在这里有我的 javascript 代码: define(['controllers/controllers', 'services/alerts'], function(module) {
的意义是什么scope = scope-token *( SP scope-token ) scope-token = 1*( %x21 / %x23-5B / %x5D-7E ) 在 RFC6749
我是 AngularJS 的新手。我试图找出这两个 Controller 定义之间的区别: app.controller('simpleController', ['$scope', function
似乎所有 Guice 的开箱即用 Scope 实现本质上都是基于线程的(或完全忽略线程): Scopes.SINGLETON和 Scopes.NO_SCOPE忽略线程并且是边缘情况:全局范围和无范围。
如果这个问题涉及的是一个常见问题,我很抱歉,但我发现这个问题非常抽象,并且无法真正为其构建一个好的 Google 搜索词。 我试图理解并找到 Maven 中提供的依赖项的用例。我的想法是这样的: 假设
假设我有以下 Controller angular.module('scopeExample', []) .controller('MyController', ['$scope', func
当前在TmThemeEditor上注册的243种配色方案中, 我注意到几乎没有人使用范围选择器运算符。 对于以下情况,运算符非常有用: (text.html | text.xml) & (meta.t
我有一些行为不符合预期的代码......我在 AngularJS Controller 中有一个事件监听器,如下所示: $scope.$on("newClipSelected", function(e
首先,如果帖子太长,我深表歉意。另外,为了以防万一这会以某种方式干扰您可能给我的答案,我不会以通常的方式定义我的 Controller 。相反,我关注http://www.technofattie.c
我有一个模式,其中许多项目类型都是“可编辑的”。这意味着我有很多模板(每种可编辑项目类型一个),这些模板期望具有唯一的字段,但具有通用功能(编辑、保存、取消编辑、删除等)。这些常见功能导致 Contr
$evalAsync 和 $applyAsync 之间有什么区别?我的理解是,当我从指令中使用 $evalAsync 时,表达式将在浏览器呈现之前进行计算。 举个例子,如果我想滚动到页面上的特定位置但
我试图为一个 $scope 变量提供另一个 $scope 变量的值。有人能告诉我出了什么问题吗?查看简单的 plunker 了解详细信息: http://plnkr.co/edit/TlKnd2fM5
我有以下一段 Angular 代码 $scope.prepare = function(){ $scope.elems = [1,2,3]; }; $scope.action = functio
我正在关注 Angularjs 的官方教程,但我陷入了第 2 步。 这是一个片段,我不明白 $scope:scope 的含义, describe('PhoneListCtrl', function()
根据文档, Global: Component is shared among all users. Session: Separate instances of the component are
显示作用域变量,类似于 Angularjs 中的 ng-repeat 元素 这些是我的范围变量 $scope.published = true; $scope.count = 3; 我还有一个名为 l
我是 Angular 的新手,我想在普通的 javascript 中做一些非常简单的事情,但我无法找到如何在 Angular 中正确地做到这一点! 我想设置一个通用函数来清除输入文本字段: 我有这个
在article中发现了这样一个idea : Notice how the value function takes the scope as parameter (without the $ in
注释部分将位于 $scope.$on 下。我需要将 options 返回到我保存 $scope.$emit 的地方。请帮助!!! if (gridConfig.Batch) {
我有一个带有 2 个作用域的 Controller : app.controller('search', function($scope) { $scope.value1 = '';
我是一名优秀的程序员,十分优秀!