【问题标题】:Unittest with angular, karma and es6 - injecting a service使用 angular、karma 和 es6 进行单元测试 - 注入服务
【发布时间】:2016-12-21 17:46:45
【问题描述】:

我正在尝试为此指令编写测试:

export default angular.module('app.page.section.accountBalance', [])
    .directive('accountBalance', accountBalanceConfig);

function accountBalanceConfig() {
    return {
        restrict: 'E',
        replace: true,
        scope: {
            data: '='
        },
        template: require('./account-balance.tpl.html'),
        controller: accountBalanceController,
        controllerAs: 'accountBalance'
    }
}

class accountBalanceController {
    constructor($scope, domFactory, userService, $filter) {
       // controller logic
    }
}

如您所见,指令的控制器依赖于domFactory,这是我创建的用于编译 DOM 的自定义服务:

export class DomGenerator {
    /**
     * @constructor
     * inject dependencies
     */
    constructor() {
        'ngInject';
    }

    // service logic
}

这是测试:

import { DomGenerator } from '../../shared/services/dom-genereator/dom-generator.srv';
import compile from '../compile/compile.drv';
import module from './account-balance.drv';

describe('account balance', () => {
    var $rootScope, $compile, $location, $window, $document, userService, domFactory, $provide;

    beforeEach(() => {
        domFactory = new DomGenerator();
        userService = new UserData();
    });

    beforeEach(angular.mock.module(compile.name));
    beforeEach(angular.mock.module(module.name));

    beforeEach(inject(($injector) => {
        $rootScope = $injector.get('$rootScope');
        $compile = $injector.get('$compile');
    }));

    it('renders account balance widget headline', () => {
        var element =  $compile(`<account-balance data="{headline: {title: 'account balance'}}"></account-balance>`)($rootScope);
        $rootScope.$digest();
        expect(element.html()).to.contain('account balance');
    });
});

class UserData {
    constructor() {

    }
}

这是我得到的错误:

Error: [$injector:unpr] Unknown provider: domFactoryProvider <- domFactory
http://errors.angularjs.org/1.5.8/$injector/unpr?p0=domFactoryProvider%20%3C-%20domFactory
    at webpack:///~/angular/angular.js:68:0 <- spec.bundle.js:16824:13
    at webpack:///~/angular/angular.js:4511:0 <- spec.bundle.js:21267:20
    at Object.getService [as get] (webpack:///~/angular/angular.js:4664:0 <- spec.bundle.js:21420:40)
    at webpack:///~/angular/angular.js:4516:0 <- spec.bundle.js:21272:46
    at getService (webpack:///~/angular/angular.js:4664:0 <- spec.bundle.js:21420:40)
    at injectionArgs (webpack:///~/angular/angular.js:4688:0 <- spec.bundle.js:21444:59)
    at Object.invoke (webpack:///~/angular/angular.js:4710:0 <- spec.bundle.js:21466:19)
    at $controllerInit (webpack:///~/angular/angular.js:10354:0 <- spec.bundle.js:27110:35)
    at nodeLinkFn (webpack:///~/angular/angular.js:9263:0 <- spec.bundle.js:26019:35)
    at compositeLinkFn (webpack:///~/angular/angular.js:8620:0 <- spec.bundle.js:25376:14)

这意味着我没有将服务注入指令的控制器。 我究竟做错了什么?

不是

beforeEach(() => {
            domFactory = new DomGenerator();
            userService = new UserData();
        });`

够了吗?

【问题讨论】:

    标签: angularjs unit-testing webpack karma-runner


    【解决方案1】:

    你应该这样做,你应该模拟服务(使用 sinon)。

    import accountBalanceController from 'path for account balance controller'
    // import accountBalanceConfig
    // import html template
    import { DomGenerator } from '../../shared/services/dom-genereator/dom-generator.srv';
    import compile from '../compile/compile.drv';
    import module from './account-balance.drv';
    import sinon from 'sinon'
    
    describe('account balance', () => {
      let $rootScope, makeController,DomGeneratorFactory,$scope,$filter,userService;
    
      beforeEach(inject((_$rootScope_,_$scope_,_$filter_) => {
        $rootScope = _$rootScope_;
        $scope = _$scope_;
        $filter = _$filter_;
        DomGeneratorFactory =  sinon.createStubInstance(DomGenerator);
        // Create mock to user Service
        makeController = () => {
          return new accountBalanceController($scope, DomGeneratorFactory, userService, $filter);
        };
      }));
    
      describe('Controller', () => {
        // controller specs
        it('has a name property [REMOVE]', () => { // erase if removing this.name from the controller
          let controller = makeController();
          expect(controller).to.have.property('name');
        });
        it('includes the intended template', () => {
          expect(accountBalanceConfig.template).to.equal(htmlTemplate);
        });
      });
    });
    

    【讨论】:

    • 谢谢!应用您的代码。也为用户服务创建了一个模拟。现在我得到Uncaught TypeError: Cannot read property 'call' of undefined at webpack:/webpack/bootstrap%208abd7032bd3ba4917b19:19:0 &lt;- spec.bundle.js:20
    猜你喜欢
    • 2023-03-18
    • 1970-01-01
    • 2019-07-01
    • 1970-01-01
    • 2020-03-14
    • 2020-01-08
    • 2016-04-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多