Angularjs 角JS单元测试

Angularjs 角JS单元测试,angularjs,Angularjs,Angular noob-停留在单元测试场景中。简言之: var app = angular.module("app"); app.controller('Ctrl', ['$scope', '$filter', '$parse', '$http', '$window', '$location', Ctrl]); function Ctrl($scope, $filter, $parse, $http, $window, $location) { $sco

Angular noob-停留在单元测试场景中。简言之:

var app = angular.module("app");

    app.controller('Ctrl', ['$scope', '$filter', '$parse', '$http', '$window', '$location', Ctrl]);
    function Ctrl($scope, $filter, $parse, $http, $window, $location)
    {
       $scope.change = function(data)
       {
          $http.post('url', p).then(function(response)
          {
              //code to process 'change'
          } 
       }
    }

//unit test
beforeEach(inject(function ($controller, $rootScope, $httpBackend) 
{
    httpBackend = $httpBackend;

    scope = $rootScope.$new();
    ctrl = $controller('Ctrl', { $scope: scope });

    httpBackendHandler =
        httpBackend.when("POST","/url").respond(createServerResponse());

}));
在我正在编写的测试中,我需要测试在http.post()之后运行的代码。目前,我调用$scope.change(),后跟$scope.apply()。
调用.then中的“内部”代码,但在执行测试结果“expect”后,返回不正确的结果。如何“超时”直到.then()代码执行完毕,以便测试可以得到准确的结果?

请忽略-问题出在其他地方。如果没有看到实际的测试,很难判断错误。但您的测试可能会失败,因为调用
$httpBackend.flush()
方法之前,
$httpBackend
不会返回结果。您应该在控制器方法调用和
$scope.apply()
之间的某个地方调用
httpBackend.flush()