jeudi 31 mars 2016

Unit testing $httpBackend angular no response defined

I'm struggling in my first test so please bear with me. I want to test a function that make a http post:

$scope.formData= 'client=' + $scope.client_selected + '&version=' + $scope.version_selected + '&modules=' + $scope.client.modules;
        var response = $http.post('http://localhost:8080/mavenproject8/lol/form', $scope.formData);
        response.success(function (data, status, headers, config) {
            $scope.validity = true;
            $scope.status = "true";
        });
        response.error(function (data, status, headers, config) {
            $scope.status = "false";
            alert("Exception details: " + JSON.stringify({data: data}));

and my test looks like this:

...
beforeEach(inject(function ($injector) {
    // Set up the mock http service responses
    $httpBackend = $injector.get('$httpBackend');
    $httpBackend.when('POST', 'http://localhost:8080/mavenproject8/lol/form', data)
            .respond([200, {success: true, errors: [{}]}]);

and this is the it block:

it('should succeed when everything is correct', function () {
    $rootScope.client.modules=["360"];
    $rootScope.version_selected="2.4.0"
    $rootScope.client_selected="NSM";
    $rootScope.formData='client=' + $rootScope.client_selected + '&version=' + $rootScope.version_selected + '&modules=' + $rootScope.client.modules;
    $httpBackend.expectPOST('http://localhost:8080/mavenproject8/lol/form',$rootScope.formData);
    $rootScope.myFunc();
    expect($rootScope.validity).toBe(true);
});

But this test doesn't pass, with the error:

Error: No response defined !

I feel like I'm so close but I can't make it work. I would be grateful if you could help me. Thank you!

Aucun commentaire:

Enregistrer un commentaire