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

孤街浪徒 提交于 2020-01-17 07:42:33

问题


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()

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


回答1:


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.




回答2:


P.S 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.



来源:https://stackoverflow.com/questions/42789183/how-to-mock-http-post-method-that-has-been-called-in-other-service-method-in-ja

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!