简体   繁体   中英

How to mock $http.post method that has been called in other service method in jasmine?

I have and angular service that I want to test. In one of his methods I am using $http of angular service. I am simply want to mock that function (to be more specific mock $http.post function) that would return whatever I want and inject this mock to my service test.

I am tried to find solution and I found $httpBackend but I am not sure that this could help me.

MyService looks like that:

angular.module('app').service('MyService' , function (dependencies) {
    let service = this;
    service.methodToTest = function () {
        $http.post('url').then(function () {
            // Do something
        });
    }
}
  • I am want to test methodToTest and inject the mock of $http.post()

PS please remember that $http.post() returns promise so I think that I need to consider in that.

this sounds like exactly what $httpBackend is for.

You might also be able to mock only $http.post by doing something like $http.post = jasmine.createSpy(); if you inject $http in your test, but I don't know.

If you do use $httpBackend , perhaps this example helps you, in your jasmine test do something like this

beforeEach(inject(function(_$httpBackend_){
  // The injector unwraps the underscores (_) from around the parameter names when matching
  $httpBackend = _$httpBackend_;

  $httpBackend.whenRoute('POST', 'url')
    .respond(function(method, url, data, headers, params) {

      expect(somevariable).toContain(params.something);

      return [200, '{"progress":601}'];
    });
}));

the $httpBackend will intercept all $http.post 's to url and execute this function. It should be just like the methodToTest submitted to the actual url and got your fake return value.

The return value indicates a success http status code (200) and returns whatever you put in the second argument as the data property of the response (here response.data == '{"progress":601}' ). Which will be in the then function. See How do I mock $http in AngularJS service Jasmine test?

The expect function there is just an example (not needed), to show you that you can put an expect clause there if you want.

PS please remember that $http.post() returns promise so I think that I need to consider in that.

The service needs to return that promise:

angular.module('app').service('MyService' , function (dependencies) {
    let service = this;
    service.methodToTest = function () {
        //$http.post('url').then(function () {
        //vvvv RETURN http promise
        return $http.post('url').then(function () {
            // Do something
        });
    }
}

When a function omits a return statement, it a returns a value of undefined . There is no way that the service can indicate success or failure to the user.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM