mercredi 24 février 2016

How to mock function for unit testing in angularJS?

function httpWrapperService(.....) {
    var self = this;
    self.get=get;
    self.httpHandler=httpHandler;


    function get(url, header) {
         var config= {url:url, header:header}
         return httpHandler(config);
    }

    function httpHandler(configObject) {
         // some stuff
    }

 }

In the above code I want to unit test get function , I dont want to invoke httpHandler function . for this I tried everything like creating SPY during initialization but its not working.

describe('Checking httpWrapperService get() method for different test cases!!!\n', function () {


        it(" Check when url and headers are blank", function (done) {
            spyOn(httpWrapperService, 'httpHandler');
            httpWrapperService.get();
            expect(httpWrapperService.httpHandler).toHaveBeenCalledWith({
            url: some url,
            headers: some header
            });

                done();
        });

  });

I observe even after mocking httpHandler the function is getting invoked, not able to understand the reason. I don't want to invoke it.What and where is the mistake I am doing . Any help is appreciated!!

Aucun commentaire:

Enregistrer un commentaire