gpt4 book ai didi

jasmine - 预期未定义要定义我该如何解决

转载 作者:行者123 更新时间:2023-12-03 20:19:33 24 4
gpt4 key购买 nike

我正在尝试使用 Jasmine 测试我的 Angular,但不知何故,我一直收到此错误 Expected undefined to be defined 并且我遵循了我在平均堆栈上的 Angular 文档示例

测试.js

describe('测试 Ecdreport Controller ', function(){
var $范围, Controller ;

var app = angular.module('mean.ecdreport',[])
.controller('EcdreportController', ['$scope', '$http', 'Global', 'Ecdreport', function($scope, $http, Global, Ecdreport) {
$scope.global = Global;
$scope.query = "";
$scope.package = {
name: 'ecdreport'
};


$scope.startDate = null;
$scope.endDate = null;
$scope.currentPage = 1;
$scope.child= [];
$scope.maxSize = 5;
$scope.items = [];
$scope.itemsPerPage = 10;
$scope.totalItems = null;
$scope.direction = 1;
$scope.directionOld = 1;
$scope.sortAttributes = 0;
$scope.sortAttributesOld = 0;
$scope.datamodel = null;


$scope.getDataModel = function() {

$http({url:'/api/v1/getdatamodel', method:"GET"})
.success(function(data) {
console.log('Datamodel successful');
$scope.datamodel = data[0];
console.log('datamodel', data);

})
.error(function(error) {
$scope.datamodel =[];
});
}
// console.log("Trying to get datamodel");
$scope.getDataModel();
});



describe('Testing Ecdreport Controllers', function(){
var $scope, controller;

beforeEach(module('mean.ecdreport', function($controllerProvider){
$controllerProvider.register('EcdreportController', function(){
});
}));


beforeEach(inject(function(_$rootScope_,_$controller_){
$scope = _$rootScope_.$new();
controller = _$controller_('EcdreportController',
{$scope : $scope
});
}));


it('Should be registered', function(){
expect(controller).toBeDefined();
})

it('Testing Scope', function(){
expect($scope).toBeDefined()
expect($Scope.getDataModel).toBeDefined();
})



});




beforeEach(module('mean.ecdreport', function($controllerProvider){
$controllerProvider.register('EcdreportController', function(){
});
}));


beforeEach(inject(function(_$rootScope_,_$controller_){
$scope = _$rootScope_.$new();
controller = _$controller_('EcdreportController',
{$scope : $scope
});
}));


it('Should be registered', function(){
expect(controller).toBeDefined();
})

it('Testing Scope', function(){
expect($scope).toBeDefined()

expect($scope.getDataModel).toBeDefined();
})

});

最佳答案

您会收到该错误,因为您的测试 Controller 从未被定义。您需要使用 var controller = ...
您应该像这样使用 Controller 注入(inject):

beforeEach(inject(function(_$rootScope_,_$controller_){
$scope = _$rootScope_.$new();
createController = function() {
return _$controller_('EcdreportController', {
$scope : $scope
});
};
}));

并像这样在每个测试中初始化 Controller :
it('Should be registered', function(){
var controller = new createController();
expect(controller).toBeDefined();
})

这样,如果您的 Controller 需要传递任何数据,您还可以在每个测试中传递不同的参数。

关于jasmine - 预期未定义要定义我该如何解决,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37654770/

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