首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何确保控制器被注入指令进行测试?

如何确保控制器被注入指令进行测试?
EN

Stack Overflow用户
提问于 2016-03-01 21:11:08
回答 1查看 805关注 0票数 0

我正在使用智能表,并试图用Karma建立一些测试。我反复得到错误Error: [$compile:ctreq] Controller 'stTable', required by directive 'removePag', can't be found!。在实际使用指令时,我没有发现任何错误。在我看来,Karma应该将智能表提供给tools模块,这正是我在规范中所指的,而不是骰子。如何确保stTable在测试中对removePag可用?

在tools.js中(包括expose,因为我怀疑可能涉及到它,因为它是一个链接到顶级st-table的指令):

代码语言:javascript
复制
angular.module('tools', ['smart-table'] );

angular
    .module('freeTools')
        .directive('expose', exposeTableState)
        .directive('removePag', removePag)

function exposeTableState(){
    return {
        require:'stTable',
            link:function(scope, element, attr, ctrl){
            scope.smartTableState=ctrl.tableState();
        }
    };
}

function removePag() {
    return {
        restrict: 'E',
        require: '^stTable',
        template: '<a href="">View as a single page</a>',
        link: function(scope, element, attrs, ctrl) {
            return element.bind('click', function () {
                return scope.$apply(function () {
                    var tableState;
                    tableState = ctrl.tableState();
                    tableState.pagination.number = tableState.pagination.totalItemCount;
                    return ctrl.pipe();
                });
            });

        }
      };
}

在toolsSpec.js中:

代码语言:javascript
复制
describe('tools', function () {
    var $compile,
    $rootScope;
    beforeEach(module('freeTools'));
    beforeEach(inject(function(_$compile_, _$rootScope_){
        $compile = _$compile_;
        $rootScope = _$rootScope_;
    }));

    it('Replaces the element with the appropriate content', function() {
        var element = $compile("<remove-pag></remove-pag>")($rootScope);
        $rootScope.$digest();
        expect(element.html()).toContain("View as a single page");
    });
}

在Karma.conf中:

代码语言:javascript
复制
module.exports = function(config){
config.set({

basePath : '../../',

files : [
    'bower_components/jquery/dist/jquery.js',
    'bower_components/angular/angular.js',
    'bower_components/angular-smart-table/dist/smart-table.js',
    'bower_components/angular-mocks/angular-mocks.js',
    'assets/scripts/*.js',
    'assets/test/*.js'
],

autoWatch : true,

frameworks: ['jasmine-ajax', 'jasmine'],

browsers : ['Chrome'],

plugins : [
        'karma-chrome-launcher',
        'karma-firefox-launcher',
        'karma-jasmine-ajax',
        'karma-jasmine',
        ],
});
};

简化的html:

代码语言:javascript
复制
<section ng-app="tools">
  <table st-table="data" st-safe-src="safe" expose>
    <div st-pagination>
      <remove-pag></remove-pag>

我意识到这个问题非常类似于单元测试AngularJS (智能表)指令。模仿智能表测试他们自己的控制器的方式也是行不通的,就像这里所建议的那样。无论如何,模块源代码中的stTable似乎是一个指令,而不是控制器。

我还尝试在测试中使用st-table包装元素,就像使用相同错误的$compile("<table st-table><remove-pag>...一样。

我已经研究过像指令要求的控制器找不到这样的问题,但是我已经有了一个共享的父级,tools

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-03-02 17:25:38

问题是,显然,父母必须被嘲笑。H/t到单元测试需要父指令的AngularJS子指令,以获得关于嘲弄的指导。

代码语言:javascript
复制
it('Replaces the element with the appropriate content', function() {
    // This can be anything, even an empty object.
    var stTableControllerMock = {
        idk: function () {
            return 'something';
        }
    };

    // Create element with dummy parent
    var element = angular.element('<fake-parent><remove-pag></remove-pag></fake-parent>');

    // '$' + {nameOfMissingController} + 'Controller' = '$stTableController'
    element.data('$stTableController', stTableControllerMock);

    // Compile, find the element we're testing, and digest
    $compile(element)($rootScope.$new());
    element = element.find('remove-pag');
    $rootScope.$digest();

    // Test
    expect(element.html()).toContain("View as a single page");
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35733591

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档