1

I am referring the stackoverflow article How do I mock a service that returns promise in Angularjs Jasmine unit test?. And found it useful in my scenario as well. But I want to load and return object from json file as mock instead of explicit object. So is it possible to achieve that?

I am referring below response:

describe('testing a method() on a service', function () {    

    var mock, service

    function init(){
         return angular.mock.inject(function ($injector,, _serviceUnderTest_) {
                mock = $injector.get('service_that_is_being_mocked');;                    
                service = __serviceUnderTest_;
            });
    }

    beforeEach(module('yourApp'));
    beforeEach(init());

    it('that has a then', function () {
       //arrange                   
        var spy= spyOn(mock, 'actionBeingCalled').and.callFake(function () {
            return {
                then: function (callback) {
                    return callback({'foo' : "bar"});
                }
            };
        });

        //act                
        var result = service.actionUnderTest(); // does cleverness

        //assert 
        expect(spy).toHaveBeenCalled();  
    });
});
Community
  • 1
  • 1
Sumit Deshpande
  • 2,155
  • 2
  • 20
  • 36

0 Answers0