1

我有以下问题:我无法从 angularjs 单元测试文件访问控制器中对象文本的属性文本

$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 不正确并且与预期的相同。text

我不知道任何想法可能是什么问题?建议?

4

1 回答 1

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
}));

这是为了$scope在http调用之后触发同步。
=> 手动测试,自动生产运行。

于 2013-12-25T18:39:08.963 回答