Angular测试控制器使用模拟工厂返回promise

时间:2016-06-10 17:29:32

标签: javascript angularjs testing karma-jasmine ngmock

我试图测试Angular控制器并模拟工厂,以便我可以在同一测试中使用它。我是Angular测试的新手。一直无法弄清楚如何做到这一点。我的工厂,没有使用$ http而不是$ q服务,返回一个承诺。鉴于工厂的函数调用返回了一个承诺,我也不确定在我的模拟工厂里放什么。

我的最终目标是从我的控制器调用我的模拟工厂,然后检查控制器中的两个数组以获取应该填充它们的数据。如果您有关于重构我的可测试性测试的任何提示,请提供反馈。

角度控制器

    export class workListController {
      constructor(dataService, $q) {
        this.$q = $q;
        this.work = [];
        this.tasks = [];
        this.dataService = dataService;
        this.setup();
      }

      setup() {
        this.$q.all([this.dataService.getWorkItems(), this.dataService.getTasks()])
        .then(() => {
          this.work = this.dataService.getState().work;
          this.tasks = this.dataService.getState().tasks;
          this.dataService.addNumberOTasksToWork();
        });
      }

      tasksForWork(workId) {
        var workTasks = [];
        for (let task of this.tasks) {
          if (task.agf__Work__c === workId) {
            workTasks.push(task);
          }
        }
        return workTasks;
      };

    }

Angular Factory

  const dataService = ($q) => {
    let work = [];
    let tasks = [];
    let connection = new Connection{/**/};

    return { getWorkItems, getTasks, addNumberOTasksToWork, getState};
    function queryWrapper(query) {
      var deferred = $q.defer();
      connection.query(query)
        .then(function(result) {
          deferred.resolve(result);
        }, function(error) {
          deferred.reject(error);
        });

      return deferred.promise;
    }


    function getWorkItems() {
      return queryWrapper(`SELECT Id, ......`)
       .then((data) => {
         //data looks like this: {totalSize: 3, done: true, records: [......]}
         work = data.records;
       });
    }

    function getTasks() {
      return queryWrapper(`SELECT Id,...`)
       .then((data) => {
         //data looks like this: {totalSize: 3, done: true, records: [......]}
         tasks = data.records;
       });
    }

    function addNumberOTasksToWork() {
      work.forEach((workItem) => {
        workItem.numberOfTasks = 0;
      });
      work.forEach((workItem) => {
        tasks.forEach((taskItem) => {
          if (taskItem.agf__Work__c === workItem.Id) {
            workItem.numberOfTasks++;
          }
        });
      });
    }

    function getState(){
      return {work,tasks};
    }

  };

  export {dataService};

测试文件

  import {workList} from './work-list.module.js';
  import {workListDirective} from './work-list.directive.js';
  import template from './work-list.html';
  import {workListController} from './work-list.controller.js';

  describe('AA_TaskBoard -  workList', function () {
    let $scope;
    let $controller;
    let $httpBackend;
    let mockDataService;

    beforeEach(angular.mock.module(workList.name));
    //trying to mock factory
    beforeEach(angular.mock.module(function($provide) {
      $provide.value('dataService', mockDataService);
      mockDataService = {
        getWorkItems: function(){
          //this should return a promise, but unsure of what to put here
          return {

          };
        },
        getTasks: function(){
          return {

          };
        }
      };

    }));


    beforeEach(inject(function(_$rootScope_, _$controller_, _$httpBackend_) {
      $rootScope = _$rootScope_;
      $controller = _$controller_;
      $httpBackend = _$httpBackend_;
    }));





  });

0 个答案:

没有答案