我是测试的新手,我正在用茉莉花测试一些角度代码。我创建的模拟承诺并没有解决。这是我正在测试的代码块:
$scope.init = function() {
EngageService.getGrouping($stateParams.gid).then(function(res) {
$scope.grouping = res.data;
$scope.parent_id = $scope.grouping.parent != null ? $scope.grouping.parent.id : null;
$scope.startDate = $scope.grouping.startDate ? new Date($scope.grouping.startDate) : new Date()
$scope.endDate = $scope.grouping.endDate ? new Date($scope.grouping.endDate) : new Date()
$scope.nda = $scope.grouping.nda == "No" ? false : true;
$scope.grouping.invitation = $scope.grouping.invitateOnly == true ? 'true' : 'false';
$scope.canManageGrouping = UserService.memberships[$scope.grouping.id].role.canManageGrouping;
$scope.canCreateGroup = UserService.memberships[$scope.grouping.id].role.canCreateGroup;
})
EngageService.getProjects().then(function(res) {
$scope.projects = res.data.rows;
})
}
这是测试承诺的代码
describe('Tests for WorkspaceEdit', functio
var getGroupingPromise;
beforeEach(function() {
module('iq4App');
return inject(function($injector) {
this.EngageService = {
getGrouping: function(gid) {
getGroupingPromise = $q.defer();
console.log(getGroupingPromise.promise)
return getGroupingPromise.promise;
}
};
this.scope = this.rootScope.$new();
this.scope.$digest();
}); '$upload': this.upload,
'ResourceService': this.ResourceService,
'MatchService': this.MatchService
});
this.scope.$digest();
});
});
});
it('should set stuff on init', function() {
var fakeData = {
data: {
parent: {
id: 3
}
}
}
this.scope.init();
this.scope.$digest();
getGroupingPromise.resolve(fakeData.data)
expect(this.scope.grouping).toBe(fakeData.data);
})
});
我正在尝试将一些假数据解析为$ scope.grouping,但数据正在解析为未定义。我知道这个承诺是在this.EngageService的getGrouping方法中从我的console.log创建的。我已经删除了许多我认为不相关的代码,但如果您需要更多详细信息,请告诉我。
谢谢!
答案 0 :(得分:1)
您需要使用虚假数据解决承诺:
getGroupingPromise = $q.defer();
getGroupingPromise.resolve(fakeData.data);
return getGroupingPromise.promise;
答案 1 :(得分:1)
我的测试中的摘要必须遵循决心,而不是之前。