gpt4 book ai didi

angularjs - Jasmine 测试中的引用错误 : Can't find variable

转载 作者:行者123 更新时间:2023-11-28 20:14:19 25 4
gpt4 key购买 nike

我的 Angular 应用程序中有一个简单的 Controller ,同样的 jasmine 测试规范返回一个引用错误。我的 Controller 代码:

'use strict';
angular.module('taskListAppApp')
.controller('MainCtrl', function ($scope) {
$scope.todoList = [{
todoText: 'In case of Fire',
done: false
}, {
todoText: 'git commit',
done: false
}, {
todoText: 'git push',
done: false
}, {
todoText: 'exit the building!',
done: false
}];


$scope.getTotalTodos = function () {
return $scope.todoList.length;
};


$scope.todoAdd = function () {
// Checking for null or empty string
if (null !== $scope.taskDesc && "" !== $scope.taskDesc) {
$scope.todoList.push({
todoText: $scope.taskDesc,
done: false
});
}
};

// Function to remove the list items
$scope.remove = function () {
var oldList = $scope.todoList;
$scope.todoList = [];
angular.forEach(oldList, function (x) {
if (!x.done) {
$scope.todoList.push(x);
}
});
};
});

还有我的测试规范:

    "use strict"

describe('Controller: MainCtrl', function () { //describe your object type
// beforeEach(module('taskListNgApp2App')); //load module
beforeEach(angular.mock.module('taskListAppApp'));
describe('MainCtrl', function () { //describe your app name

var todoCtrl2;
beforeEach(inject(function ($controller, $rootScope) {
var scope = $rootScope.$new();
todoCtrl2 = $controller('MainCtrl', {
//What does this line do?
$scope: scope
});
}));

it('should have todoCtrl defined', function () {
expect(todoCtrl2).toBeDefined();
});

it('trial test for toEqual', function(){
var a = 4;
expect(a).toEqual(4);
});
//THESE 2 FAIL
it('should have todoList defined', function() {
expect(scope.todoList).toBeDefined();
});

it('should have add method defined', function(){
expect(todoCtrl2.todoAdd()).toBeDefined();
});

});
});

我得到的错误是:

PhantomJS 2.1.1 (Linux 0.0.0) Controller: MainCtrl MainCtrl should have add method defined FAILED
TypeError: undefined is not a function (evaluating 'todoCtrl2.todoAdd()') in test/spec/controllers/main.spec.js (line 58)
test/spec/controllers/main.spec.js:58:28
loaded@http://localhost:8080/context.js:151:17
PhantomJS 2.1.1 (Linux 0.0.0): Executed 4 of 4 (2 FAILED) (0.05 secs / 0.02 secs)

我尝试了其他方法来调用对象/函数,但最后 2 次测试每次都失败并出现相同的错误,即。引用错误

我要去哪里调用对象?

最佳答案

您需要在函数外声明var scope。您的范围变量在您的测试用例中未定义。

试试这个

describe('Controller: MainCtrl', function () {      //describe your object type
var scope;
// beforeEach(module('taskListNgApp2App')); //load module

beforeEach(angular.mock.module('taskListAppApp'));
describe('MainCtrl', function () { //describe your app name

var todoCtrl2;
beforeEach(inject(function ($controller, $rootScope) {
scope = $rootScope.$new();
todoCtrl2 = $controller('MainCtrl', {
//What does this line do?
$scope: scope
});
}));

it('should have todoCtrl defined', function () {
expect(todoCtrl2).toBeDefined();
});

it('trial test for toEqual', function(){
var a = 4;
expect(a).toEqual(4);
});
//THESE 2 FAIL
it('should have todoList defined', function() {
expect(scope.todoList).toBeDefined();
});

it('should have add method defined', function(){
expect(scope.todoAdd).toBeDefined();
});

});
});

关于angularjs - Jasmine 测试中的引用错误 : Can't find variable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39072441/

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