unit testing two service which are in different mo

2019-09-14 19:44发布

问题:

I have written some service in angular. Check this PLUNKER.

Injecting CommonService, $rootRouter, ModalService in RouteService.

Mind the module :

  • CommonService is in mysampleapp.core

  • RouteService is in mysampleapp

Also when I try to do this

beforeEach(module('mysampleapp.core'));
beforeEach(module('mysampleapp'));

It gives me some weird ReferenceError: Can't find variable: Map (line 2166) error. Do i need to do the above or am doing it in wrong way??

I am stuck with unit testing RouteService. You can see sample spec file at PLUNKER.

How to test goTo and getActivePage methods in RouteService?

Here is code.

First service is RouteService

'use strict';

angular.module('mysampleapp.core')
.service('RouteService',
  function(CommonService, $rootRouter, ModalService) {

    console.log('RRRRRRRRRRRRRRRRRRRRRRRRRRRoute');

    return {
      goTo: goTo,
      getActivePage: getActivePage
    };

    function goTo(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;
      }
    }

    function getActivePage() {
      return CommonService.getProperty('activeMenu');
    }

  });

回答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() {

        var RouteService, 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
        };

        function goTo(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;
            }
        }

        function getActivePage() {
            return CommonService.getProperty('activeMenu');
        }
    });

Hope this helps!