0

I'm using angular-mock to simulate an httpBackend for an Angular app. The response status should depend on the posted data.

I'm not using this for testing, but as a full frontend version of a fullstack (PHP-Angular) application.

Is this even possible ?

$http.post('/play', {move:$scope.move})
    .then(
        // success
        function(result) {
            angular.forEach(result.data, 
                function(index) {
                    // do something with the data
                }
            );
        },
        // error: increment error_counter
        function() {
            $scope.error_count ++;
        }
    )
    .then(
        function() {
            // do some extra stuff
        }
    );
}
  • What do you mean exactly? with `$httpBackend` you can already make dynamic responses (https://docs.angularjs.org/api/ngMock/service/$httpBackend#dynamic-responses) depending on the input. the `respond()` function can take a callback giving you posted data, header,... – Sonaryr Dec 08 '15 at 13:27
  • I know that with $httpBackend I can Mock the backend service like this: $httpBackend.when('POST', '/play') .respond(200, { ... } ); – Mauricio van der Maesen Dec 08 '15 at 15:44
  • But I don't know how to modify the "200" status to, for example "400' , depending on the posted data – Mauricio van der Maesen Dec 08 '15 at 15:46
  • With a callback, please read the documentation – Sonaryr Dec 08 '15 at 16:05

1 Answers1

0

This works:

app.run(function($httpBackend) {
    $httpBackend.whenPOST('/play')
        .respond(function(method, url, json) {
            var data = angular.fromJson(json);
            // process data ...
            var status = XXX
            var return_data = {};
            return [status, return_data, {}];
    }
}