单元测试角度服务无法达到服务+茉莉花内的功能 [英] unit test angular service not able to reach function inside service + jasmine

查看:107
本文介绍了单元测试角度服务无法达到服务+茉莉花内的功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

RouteService 中注入 CommonService,$ rootRouter,ModalService

我坚持使用单元测试这些服务。您可以在 PLUNKER 上查看样本规范文件。

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

编辑:无论我在plunker进行的任何测试都没有按预期工作。我不确定我做错了什么。

如何测试 goTo getActivePage 中的方法> RouteService

如何测试 getProperty setProperty CommonService 中的方法?

How to test getProperty and setProperty methods in CommonService?

以下是代码。

第一项服务是 RouteService

'use strict';

angular.module('mysampleapp')
.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');
    }

  });

另一个是 CommonService

'use strict';

angular.module('mysampleapp')
.service('CommonService',
  function() {

    var obj = {
      /* All page validation check before perform analysis */
      isValidationSuccess: false,
      /* Highlight the menu */
      activeMenu: 'HomeMenu'
    };


    function setProperty(key, value) {

      obj[key] = value;
    }

    function getProperty(key) {
      return obj[key];
    }

    function getAllProperties() {
      return obj;
    }

    return {
      setProperty: setProperty,
      getProperty: getProperty,
      getAllProperties: getAllProperties
    };
  }
);


推荐答案

大多数情况下服务的单元测试应该是从其他服务。如果您要测试 CommonService ,您必须模拟其他服务,例如 CommonService 等等。主要原因是您没有不得不担心如何运行其他服务,因为在此测试中,您希望其他服务能够正常工作。

Unit tests for services in most cases should be islolated from other services. If you going to testing CommonService you must mock other services, such as CommonService and etc. Main reason that you do not have to worry how to run for another service, because in this test you expecting that other services will work correctly.

describe('RouteService', function () {
'use strict';

var RouteService,
    ModalService,
    CommonService,
    mockedValue,
    $rootRouter;

beforeEach(module('mysampleapp'));

beforeEach(inject(function (_RouteService_, _ModalService_, _CommonService_, _$rootRouter_) {
    RouteService = _RouteService_;
    ModalService = _ModalService_;
    CommonService = _CommonService_;
    $rootRouter = _$rootRouter_;

    $rootRouter.navigate = jasmine.createSpy();

    ModalService.openModal = jasmine.createSpy(); //sometimes open modal return promise, and you should check it to

    CommonService.getProperty = jasmine.createSpy().and.callFake(function () {
        return mockedValue;
    });

    CommonService.setProperty = jasmine.createSpy().and.callFake(function () {
        return mockedValue;
    });

}));

it('should exist', function () {
    expect(RouteService).toBeDefined();
});

it('should get active page', function () {
    RouteService.getActivePage();

    expect(CommonService.getProperty).toHaveBeenCalled(); //this test make sens only for make you coverage 100%, in you case i mean
});

describe('goTo method', function () {
    it('should check if it is valid page', function () {
        RouteService.goTo();

        expect(CommonService.getProperty).toHaveBeenCalled();
    });

    it('should set property if page is "about as" and if it is valid page, and should navigate to this page', function () {
        mockedValue = true;

        var page = 'AboutUs';

        RouteService.goTo(page);

        expect(CommonService.setProperty).toHaveBeenCalledWith('activeMenu', page);
        expect($rootRouter.navigate).toHaveBeenCalledWith([page]);

        expect(ModalService.openModal).not.toHaveBeenCalled();
    });

    it('should open modal with error if "about as" is not valid page', function () {
        var isValid = mockedValue = false;

        var page = 'AboutUs';

        RouteService.goTo(page);

        expect(ModalService.openModal).toHaveBeenCalled();

        expect(CommonService.setProperty).not.toHaveBeenCalled();
        expect($rootRouter.navigate).not.toHaveBeenCalled();
    });

    it('should set property and navigate to page', function () {
        var page = 'Test Page';

        RouteService.goTo(page);

        expect(CommonService.setProperty).toHaveBeenCalledWith('activeMenu', page);
        expect($rootRouter.navigate).toHaveBeenCalledWith([page]);

        expect(ModalService.openModal).not.toHaveBeenCalled();
    });
  });
});

这篇关于单元测试角度服务无法达到服务+茉莉花内的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆