gpt4 book ai didi

javascript - Karma + PhantomJS TypeError : undefined is not an object (evaluating scope. 大奖)

转载 作者:行者123 更新时间:2023-11-29 21:29:39 26 4
gpt4 key购买 nike

我对单元测试还是很陌生,老实说,我什至想不出任何测试,但除非我有至少 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 的内容。

My gist

最佳答案

在你的测试用例中,范围应该是 $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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com