茉莉花测试中未定义的控制器 $scope 变量
controller $scope variables undefined in jasmine tests
我正在用 Karma 和 Jasmine 编写我的第一个 Angular 应用测试。
我有一个具有此功能的控制器:
function getMyLists() {
getLists.getOne().then(function(listOne) {
$scope.list1 = listOne[0];
list1Available = true;
});
getLists.getTwo().then(function(listTwo) {
$scope.list2 = listTwo;
list2Available = true;
});
}
getMyLists();
我在测试中Mock出了服务
describe('MainCtrl', function () {
var rootScope, scope, controller, myService;
beforeEach(angular.mock.module('myApp'));
beforeEach(inject(function($rootScope, $controller, $q) {
rootScope = $rootScope;
scope = $rootScope.$new();
myService = {
getOne: function(){
// mock promise
var deferred = $q.defer();
deferred.resolve([
{
"one": 1
},
{
"type": list
}
]);
return deferred.promise;
},
getTwo: function(){
// mock promise
var deferred = $q.defer();
deferred.resolve([
{
"two": 2
},
{
"type": list
}
]);
return deferred.promise;
}
}
controller = $controller('MainCtrl', {
$scope: scope,
myService: myService
});
}));
it('should call myService service and populate the array', function(){
scope.$digest();
expect(scope.list1.length).toBeGreaterThan(0);
});
});
效果很好,正如我所料,我通过了测试。但是当我尝试测试以下示例时,我不断收到错误。
expect(scope.list2.length).toBeGreaterThan(0);
Expected undefined to be greater than 0.
expect(list1Available).toEqual(false);
ReferenceError: Can't find variable: list1Available
就像我说的,这是我的第一次测试,所以非常感谢您的帮助!
试试 $rootScope.$digest(); //为了resolve/reject承诺
it('should call myService service and populate the array', function(){
$rootScope.$apply();
expect(scope.list1.length).toBeGreaterThan(0);
});
您需要将 list1Available && scope.list2Available 附加到 $scope 以便它们可以在控制器外部访问。
function getMyLists() {
getLists.getOne().then(function(listOne) {
$scope.list1 = listOne[0];
$scope.list1Available = true;
});
getLists.getTwo().then(function(listTwo) {
$scope.list2 = listTwo;
$scope.list2Available = true;
});
}
您的测试需要测试 scope.list1Available
expect(scope.list1Available).toEqual(false);
我正在用 Karma 和 Jasmine 编写我的第一个 Angular 应用测试。
我有一个具有此功能的控制器:
function getMyLists() {
getLists.getOne().then(function(listOne) {
$scope.list1 = listOne[0];
list1Available = true;
});
getLists.getTwo().then(function(listTwo) {
$scope.list2 = listTwo;
list2Available = true;
});
}
getMyLists();
我在测试中Mock出了服务
describe('MainCtrl', function () {
var rootScope, scope, controller, myService;
beforeEach(angular.mock.module('myApp'));
beforeEach(inject(function($rootScope, $controller, $q) {
rootScope = $rootScope;
scope = $rootScope.$new();
myService = {
getOne: function(){
// mock promise
var deferred = $q.defer();
deferred.resolve([
{
"one": 1
},
{
"type": list
}
]);
return deferred.promise;
},
getTwo: function(){
// mock promise
var deferred = $q.defer();
deferred.resolve([
{
"two": 2
},
{
"type": list
}
]);
return deferred.promise;
}
}
controller = $controller('MainCtrl', {
$scope: scope,
myService: myService
});
}));
it('should call myService service and populate the array', function(){
scope.$digest();
expect(scope.list1.length).toBeGreaterThan(0);
});
});
效果很好,正如我所料,我通过了测试。但是当我尝试测试以下示例时,我不断收到错误。
expect(scope.list2.length).toBeGreaterThan(0);
Expected undefined to be greater than 0.
expect(list1Available).toEqual(false);
ReferenceError: Can't find variable: list1Available
就像我说的,这是我的第一次测试,所以非常感谢您的帮助!
试试 $rootScope.$digest(); //为了resolve/reject承诺
it('should call myService service and populate the array', function(){
$rootScope.$apply();
expect(scope.list1.length).toBeGreaterThan(0);
});
您需要将 list1Available && scope.list2Available 附加到 $scope 以便它们可以在控制器外部访问。
function getMyLists() {
getLists.getOne().then(function(listOne) {
$scope.list1 = listOne[0];
$scope.list1Available = true;
});
getLists.getTwo().then(function(listTwo) {
$scope.list2 = listTwo;
$scope.list2Available = true;
});
}
您的测试需要测试 scope.list1Available
expect(scope.list1Available).toEqual(false);