【问题标题】:unit test angular service not able to reach function inside service + jasmine单元测试角度服务无法达到服务+茉莉花内的功能
【发布时间】:2016-10-16 16:54:13
【问题描述】:

我已经用 Angular 编写了一些服务。检查这个PLUNKER

RouteService 中注入CommonService, $rootRouter, ModalService

我坚持对这些服务进行单元测试。您可以在PLUNKER 看到示例规范文件。

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

如何测试RouteService 中的goTogetActivePage 方法?

如何测试CommonService 中的getPropertysetProperty 方法?

这是代码。

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

【问题讨论】:

标签: javascript angularjs unit-testing jasmine karma-jasmine


【解决方案1】:

在您的 plunker 中,您忘记在向其添加服务之前创建 mysampleapp 模块:

angular.module('mysampleapp', []);

CommonService 的 setter 和 getter 的测试应该很简单:

describe('CommonService', function () {
    var commonService;

    beforeEach(module('mysampleapp'));

    beforeEach(inject(function (_CommonService_) {
        commonService = _CommonService_;
    }));

    it('should set and get property', function () {
        commonService.setProperty('isValidationSuccess', 'Perform');
        expect(commonService.getProperty('isValidationSuccess')).toBe('Perform');
    });
});

【讨论】:

    【解决方案2】:

    在大多数情况下,服务的单元测试应该与其他服务隔离开来。如果您要测试CommonService,您必须模拟其他服务,例如CommonService 等。您不必担心如何运行其他服务的主要原因,因为在此测试中您期望其他服务能够正常工作.

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

    【讨论】:

    • 非常感谢您的详细解答。但不知何故,它越来越坏了。所有测试都失败并出现相同的错误,即 1. 预期未定义。 2. TypeError: undefined is not an object (evalating 'RouteService.getActivePage')
    • Expected undefined to be defined. 的根本原因是什么?那 bcoz 我的 CommonService 是在不同的模块中吗?我还尝试将CommonService 模块更改为与RouteService 模块相同,但仍然没有成功。
    • @ankitd,对不起,我很忙,我会晚点准备例子。
    • 没关系 :) 将等待您的回复。
    • @ankitd 我犯了一些错误,这个测试对我有用。如果您使用单独的模块,它也应该可以工作。示例:angular.module("parent", ["child"]).
    猜你喜欢
    • 1970-01-01
    • 2017-10-09
    • 1970-01-01
    • 2014-09-26
    • 2020-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-14
    相关资源
    最近更新 更多