如何在测试时访问组件控制器?

时间:2016-11-19 02:13:03

标签: angularjs unit-testing ngmock

我正在尝试组合一些组件的测试。我有以下

describe(`selecting an item`, () => {
    let element, $scope;

    beforeEach(module('my-app'))
    beforeEach(inject(($compile, $rootScope) => {
        $scope = $rootScope;
        element = $compile(`
            <item-selector parent-id="pid">
            </item-selector>
        `)($scope)

        //call $ctrl.activate() on the component's controller
    }))

})

item-selector的控制器采用activate()方法。我如何从测试中调用它? $scope似乎不包含任何引用。

1 个答案:

答案 0 :(得分:5)

let ctrl = element.controller('item-selector')