1

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!

1 Answer 1

1

Actually, your expected post request doesn't specify a response, that's why you see that error, so all what you have to do is:

$httpBackend.expectPOST('http://localhost:8080/mavenproject8/lol/form').respond(200,{});
//don't forget to make the flush after the function call.!!
$rootScope.myFunc();
$httpBackend.flush();

( you can dont need the $httpBackend.whenPost because that's useful when you want to create a back end definition, and specify the response( get case for example) so you can remove it, and your test will still pass.)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.