本文介绍了单元测试与茉莉花在不同模块中的两个服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在角度上写了一些服务。请查看。

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

RouteService 中注入 CommonService,$ rootRouter,ModalService

介意模块:


  • CommonService mysampleapp.core

RouteService 在<$ c $中c> mysampleapp

当我尝试这样做时

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

它给我一些奇怪的 ReferenceError:找不到变量:Map(第2166行)错误。我是否需要执行上述操作或以错误方式执行此操作?

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??

我遇到了单元测试RouteService。您可以在上查看示例规范文件。

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

如何在 RouteService goTo getActivePage 方法$ c>?

How to test goTo and getActivePage methods in RouteService?

以下是代码。

第一项服务是 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');
    }

  });


推荐答案

首先,因为你正试图测试 RouteService ,你只需要注入它所在的模块,即 mysampleapp.core 。这就是我将在这里做的事情。所以这应该是你的测试案例 RouteService

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();
        });
    });

此外,因为没有 mysampleapp.core的指定定义,我冒昧地定义它。我将 mysampleapp ngComponentRouter 指定为依赖项。具体如下:

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');
        }
    });

希望这有帮助!

这篇关于单元测试与茉莉花在不同模块中的两个服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-16 23:22