我有以下问题: 我无法从angularjs单元测试文件中访问控制器中对象 textes 的属性文本:
$scope.ocr = function(source) {
initialiseZones();
$http.post("/oceriser", {
sourceImage: source
}).success(function(data, status, headers, config) {
$scope.textes = {
source: source,
editor: $scope.addEditor(angular.fromJson(data)),
text: angular.fromJson(data)
};
$scope.currentImage.source = "";
$scope.showEditor = true;
$scope.msg = "ok";
}).error(function(data, status, headers, config) {
$scope.msg = "ko";
});
}
在我的测试文件中,我想测试我的控制器方法,所以我执行以下操作:
it('has to return text from image', inject(function($httpBackend) {
var expected = {text : 'blabla'};
$httpBackend.expectPOST('/oceriser').respond(expected);
$scope.ocr('./image.png');
$httpBackend.flush();
expect($scope.textes.text).toBe(expected.text); // this line has to be false because the value of $scope.textes.text is different from expected.text value.
expect($scope.textes.source).toBe('./image.png');// this line is true and when the parameter of the matcher is changed it becomes false
}));
问题是我访问的值($ scope.textes.source是正确的./image.png)但值$ scope.textes.text不正确并且与expected.text
我不知道有什么问题会出现什么问题?建议?
答案 0 :(得分:0)
尝试在$scope.$digest()
:
$httpBackend.flush();
it('has to return text from image', inject(function($httpBackend) {
var expected = {text : 'blabla'};
$httpBackend.expectPOST('/oceriser').respond(expected);
$scope.ocr('./image.png');
$httpBackend.flush();
$scope.$digest(); //here
expect($scope.textes.text).toBe(expected.text); // this line has to be false because the value of $scope.textes.text is different from expected.text value.
expect($scope.textes.source).toBe('./image.png');// this line is true and when the parameter of the matcher is changed it becomes false
}));
这是为了在http调用之后触发$scope
同步
=>手动测试,自动生产运行。