unit testing two service which are in different module with jasmine

痴心易碎 提交于 2019-12-25 08:14:25

问题


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!



来源:https://stackoverflow.com/questions/40084091/unit-testing-two-service-which-are-in-different-module-with-jasmine

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!