0

I have defined a method for my controller, like

$scope.submitForm = ( username, password ) =>{
    $http({ 
        method: 'post',
        url: 'balabala',
        params: {username, password}
    }).success( res => { /* */ } );
}

My test specs look like:

descript('myController', () => {
    beforeEach(module('myModule'));

    let controller, httpBackend, http, scope;

    beforeEach(inject(($controller, $httpBackend, $http, $scope) => {
        scope = $scope.$new();
        httpBackend = $httpBackend;
        http = $http;
        controller = $controller;

        httpBackend.when('POST', '/login')
            .respon({
                result: 'ok'
            });
    }));

    it('should POST login', () => {
        httpBackend.expectPOST('/login');
        const myController = controller('myController', {
            $scope: scope,
            $http: http
        });
        scope.submitForm();
        httpBackend.flush();
    });
});

How can I make sure that username and password have been posted?

//EDIT: How do I assess the body of POST request?

const data = { foo: "1", bar: { x: "2" } };

httpBackend.expectPOST('http://example.com').respond((method, url, data, headers, params) => {
    console.log(method, url, data, headers, params);
});

http({
    url: 'http://example.com',
    method: 'POST',
    data
});

The output is:

'POST', 'http://example.com', 'foo=1&bar=2', Object{Accept: 'application/js on, text/plain, /', Content-Type: 'application/x-www-form-urlencoded;charset=u tf-8'}, undefined

1 Answer 1

0

Add a response value associated to post i.e

httpBackend.expectPOST('/login').respond({success:true});

And don't use when because expectPost will make sure that you are calling that post method if not it will throw an error if you are getting no errors then assume that post method has been called.

$httpBackend .whenGET('/testpath/testurl/z/values?max=10&min=1').respond(function(method, url, data, headers, params) {

// params will be {
//   max: 10,
//   min: 1
// }

});

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

7 Comments

Do you know how to get params and body when using httpBackend?
One question, How do I assess the body of POST request? I have edited my question..
you can't do directly you need to have a get call.
get call? what is it?
Maybe it's because I'm using an old version of angular 1.2.6
|

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.