Skip to content Skip to sidebar Skip to footer

Unit Testing Two Service Which Are In Different Module With Jasmine

I have written some service in angular. Check this PLUNKER. Injecting CommonService, $rootRouter, ModalService in RouteService. Mind the module : CommonService is in mysampleap

Solution 1:

First of all, since you're trying to test RouteService, you just need to inject the module it is in, i.e. mysampleapp.core. So that's what I'll be doing here. So this should be you test case for RouteService:

/**
     * This code is copyright (c) 2016 DELLEMC Corporation
     */    'use strict';

    describe('RouteService', function() {

        varRouteService, ModalService, CommonService;
        // You don't need these. So commenting these out.//     mockedValue, commonServiceSpy, RouteServiceSpy;beforeEach(module('mysampleapp.core'));

        // Wasn't sure what these are, so commented these out.// beforeEach(module('basic-unity-replication-sizer-ui.core'));// beforeEach(module('basic-unity-replication-sizer-ui'));beforeEach(inject(function(_RouteService_, _ModalService_, _CommonService_, $rootRouter) {
            RouteService = _RouteService_;
            ModalService = _ModalService_;
            CommonService = _CommonService_;
            $rootRouter.navigate = jasmine.createSpy();
        }));

        // This should pass.it('should exist', function() {
            expect(RouteService).toBeDefined();
            expect(angular.isFunction(RouteService.goTo)).toBeTruthy();
            expect(angular.isFunction(RouteService.getActivePage)).toBeTruthy();
        });
    });

Also since there wasn't a specified definition of mysampleapp.core, I took the liberty to define it. I specified mysampleapp and ngComponentRouter as dependencies. This is how:

angular.module('mysampleapp.core', ['mysampleapp', 'ngComponentRouter']).service('RouteService', function(CommonService, $rootRouter, ModalService) {
        return {
            goTo: goTo,
            getActivePage: getActivePage
        };

        functiongoTo(page) {
            var valid = CommonService.getProperty('isValidationSuccess');

            switch (page) {
                case'AboutUs':
                    if (valid) {
                        CommonService.setProperty('activeMenu', page);
                        $rootRouter.navigate([page]);
                    } else {
                        ModalService.openModal('Analysis Error', 'Complete Application Group configuration prior to running analysis.', 'Error');
                    }
                    break;

                default:
                    CommonService.setProperty('activeMenu', page);
                    $rootRouter.navigate([page]);
                    break;
            }
        }

        functiongetActivePage() {
            return CommonService.getProperty('activeMenu');
        }
    });

Hope this helps!

Post a Comment for "Unit Testing Two Service Which Are In Different Module With Jasmine"