I am facing a challenge with fetching the response from an endpoint URL that requires a POST request for login authentication. Upon adding a request payload, I should receive either successful login credentials or an error. However, I am encountering difficulties in retrieving the response.
Below is my spec file:
describe('Service: AuthFactory',function(){
beforeEach(function () {
module('ui.router');
module('users');
module('main');
});
var AuthFactory, httpBackend;
beforeEach(inject(function($httpBackend, $rootScope, $controller, _AuthFactory_){
httpBackend = $httpBackend;
AuthFactory = _AuthFactory_;
}));
it('Return a POST response from a Service function', function() {
var url = "http://localhost:3000";
var dataObj = JSON.stringify({
inputUser: { user: "TestCase1" },
inputPass: { password: "TestPass1" }
});
httpBackend.expect('POST', url + '/api/AuthService/signIn', dataObj)
.respond({});
AuthFactory.signIn(dataObj).success(function(response) {
console.log(response);
// outputs Object {}
// when in reality it should
// output the response to POST
// eg: { "login": { "user": "Test_User", "time": 54935934593 }, "outputHead": { "token": asjfjj234kfAd } }
});
httpBackend.flush();
expect(true).toBe(true);
});
});
Here is the Service
:
angular.module('users').factory('AuthFactory', ['$http', function($http) {
var AuthFactory = {};
AuthFactory.signIn = function(data) {
return $http.post('http://127.0.0.1:3000/api/AuthService/signIn', data);
};
AuthFactory.signOut = function(data) {
return $http.post('http://127.0.0.1:3000/api/AuthService/signOut', data);
};
return AuthFactory;
}]);
While the test passes successfully, the console.log()
displays Object{}
.
Interestingly, when using Postman, a Chrome extension, and making a POST request, I receive the expected login credentials in the response. So why does it work on Postman but not on my AngularJS
Jasmine unit test?