skip to Main Content

I’m trying to increase code coverage in my project by performing simple jasmine tests on basic methods. In my controller, I have this:

function fetchStates() {
            $repository.states().all().then(function(data) {
                vm.states = data;
            });
        }

I’m trying to set vm.states, but absolutely nothing I’ve tried will get that THEN to fire. I can mock a repository such that jasmine doesn’t complain that it doesn’t exist, but when i try to run controller.fetchStates(); it simply will not get to that inner line of code. What’s the correct way to mock $repository.states().all().then so that it will allow vm.states = data?

 function GetMockFacRepo() {
        var facilityPromise = new Promise((resolve, reject) => {
            resolve({ id: 1 });
        });
        return {
            states: function () {
                return {
                    id: 0,
                    all: function () {
                        then:
                        {
                            return facilityPromise
                        }
                    }
                    
                }
            }
        };
    }
var repo2 = GetMockFacRepo();
spyOn(repo2.states.all, "then").and.callFake(function () { return 1 });

2

Answers


  1. Chosen as BEST ANSWER

    It ended up being a combination of explicitly mocking the repository, creating my own promise instead of using q, and calling the controller constructor with async.

    repo code:

    var mockRepo = {
                states: function () {
                    return {
                        all: function () {                        
                            var items = [];
                            var i = 0;
                            for (i = 0; i < 5; i++) {
                                items.push(i);
                            }
                            var sectionPromise = new Promise((resolve, reject) => {
                                resolve(items);
                            });                        
                            return sectionPromise;
                        }
                    }
                },
    }
    

    Test:

     describe('POC', function () {
            beforeEach(async function () {
                controller = createController();
            });
            it('POC', function () {
                expect(controller.states.length).toEqual(5);
                
            });       
        });
    

    EDIT: The controller calls the method that populates the states property internally, I don't have to call it explicitly to get the property populated


  2. Try the following:

     function GetMockFacRepo() {
            var facilityPromise = new Promise((resolve, reject) => {
                resolve({ id: 1 });
            });
            return {
                states: function () {
                    return {
                        id: 0,
                        all: function() {
                           // return facilityPromise here so .then works
                           return facilityPromise;
                        }
                        
                    }
                }
            };
        }
    

    You also maybe have to wait for the promise to resolve before asserting vm.states = data;. In Angular 2+, you can use fakeAsync/tick and fixture.whenStable(). I am not sure what you can use in Angular 1.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search