javascript - 如何测试这个组件/模板? :)

标签 javascript angularjs jasmine karma-jasmine angularjs-components

我正在寻求有关我的 UNITEST for AngularJS(和 nvd3)问题的任何帮助。

确实,当我尝试制作图表时,我遇到了错误。 就像 $ctrl.data$ctrl.options 完全是空的......

我不明白问题出在哪里,我一整天都在尝试解锁,但没有成功..

如果有人知道问题出在哪里,我提前表示感谢。

代码:

myComp.comp.js:

angular.module('myMod')
        .component('myComp', {
            template: '<div><nvd3 options="$ctrl.options" data="$ctrl.wrapped"></nvd3></div>',
            bindings: {
                data: '<',
                zoom: '<'
            },
            controller: myCtrl
        })

myCtrl.$inject = ['$scope']
function myCtrl($scope) {
    //Here I set wrapped with data
    //and set the options for nvd3
}

myComp.spec.js:

describe('component: myComp', function() {

    beforeEach(module('myComp'));

    var $ctrl,
        $bindings,
        $template,
        scope,
        elmt;

    beforeEach(inject(function($compile, $componentController, $rootScope, $document) {

        $scope = $rootScope.$new();

        $bindings = {
            zoom: true,
            data: [ /*DATA SAMPLE HERE*/ ]
        }

        $ctrl = $componentController('myComp', $scope, $bindings);
        elmt = angular.element('my-comp data="$ctrl.data" zoom="$ctrl.zoom"></my-comp>');
        elmt = $compile(elmt)($scope);

        $scope.$apply();
    }));

    it("test", function () {
        expect(1).toBe(1);
    });
});

karma 报应/ Jasmine :

Chrome 53.0.2785 (Mac OS X 10.12.0) component: myComp test FAILED
    TypeError: Cannot read property 'ordinal' of undefined
        at Object.nv.models.multiBarHorizontal (bower_components/nvd3/build/nv.d3.js:8823:23)
        at Object.nv.models.multiBarHorizontalChart (bower_components/nvd3/build/nv.d3.js:9180:30)
        at Object.updateWithOptions (bower_components/angular-nvd3/dist/angular-nvd3.js:106:72)
        at Object.refresh (bower_components/angular-nvd3/dist/angular-nvd3.js:53:39)
        at bower_components/angular-nvd3/dist/angular-nvd3.js:449:68
        at bower_components/angular-nvd3/dist/angular-nvd3.js:517:43
        at Scope.$digest (bower_components/angular/angular.js:17524:23)
        at Scope.$apply (bower_components/angular/angular.js:17790:24)
        at Object.<anonymous> (ml.report/variableImportances/variableImportances.spec.js:38:9)
        at Object.invoke (bower_components/angular/angular.js:4718:19)
    Error: Declaration Location
        at window.inject.angular.mock.inject (bower_components/angular-mocks/angular-mocks.js:3047:25)
        at Suite.<anonymous> (myComp.comp.js:'XX':'XX')
        at myComp.spec.js:'XX':'XX'
        at myComp.spec.js:'XX':'XX'
        /* I have put 'XX' in place of the real lines / col

最佳答案

您错过了开头<在您的模板示例中。

但是,如果您使用 $componentController您不需要编译任何模板,因此可以这样做:

$ctrl = $componentController('myComp', $scope, $bindings);

或者,执行:

$scope.zoom = true;
$scope.data = [ /*DATA SAMPLE HERE*/ ];
elmt = angular.element('<my-comp data="data" zoom="zoom"></my-comp>');
elmt = $compile(elmt)($scope);

$scope.$apply();
$ctrl = elmt.controller('myComp');

关于javascript - 如何测试这个组件/模板? :),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40222733/

相关文章:

javascript - iOS 中的 jQuery 循环错误

javascript - 在《 Jasmine 》中使用 spy

jasmine - AfterEach 和 BeforeEach 仅用于描述()

node.js - 使用 Jasmine 和 Express 模拟请求对象

javascript - 显示基于选择菜单选项的 JavaScript 计算

javascript - 带 Bootstrap 弹出模式菜单的选项卡索引

javascript - 设置 css 属性的最快方法?

javascript - AngularJS/JavaScript Splice - 总是从数组中删除第一个或最后一个项目

javascript - Ember - 嵌套 View 中的异步数据

angularjs - 如何在 AngularJS 中打开/关闭 $log.debug