I have created a method in my controller that looks like this:
$scope.submitForm = ( username, password ) =>{
$http({
method: 'post',
url: 'balabala',
params: {username, password}
}).success( res => { /* */ } );
}
The test specifications for myController are as follows:
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();
});
});
What steps can I take to ensure that username
and password
are successfully posted?
//EDIT: How can I check the body of the 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 will be:
'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