相关疑难解决方法(0)

如何使用编译元素在单元测试中访问controllerAs命名空间?

在这个小提琴http://jsfiddle.net/FlavorScape/fp1kktt9/我尝试在控制器上设置属性,而不是直接在$ scope.在模板中(在生产中)我们只做myAliasCtrl.somePropertyList和ng-repeat工作.

但是,这在测试中不起作用.我不知道如何在控制器上获取/分配属性.

更新: 我的测试环境中一定有一些奇怪的本地化问题,因为我确实让ng-repeat工作.注意有两个子元素,即使该属性在别名控制器上.http://jsfiddle.net/FlavorScape/2adn983y/2/

但是,我的问题仍然是,我如何获得对该编译控制器的引用来说,创建一个间谍?(或者只是获得别名控制器的引用)?

这是来源:

angular.module('myApp', [])
.directive('myTestDirective', function () {
    return {
        restrict: 'E',
        scope: {
            myBinding: '='
        },
        replace: true,
        template: '<div ng-if="isRendered">TEST<div ng-repeat="foo in myCtrl.fooList">{{foo}}</div></div>',
        controller: 'myTestController',
        controllerAs: 'myCtrl'
    };
})
.controller('myTestController', function($scope) {
    $scope.isRendered = true;
     // if i reference this, ng-repeat works
    $scope.fooList = ["boob","cat", "Tesla"];
});

describe('myTest directive:', function () {
    var scope, compile, validHTML;

    validHTML = '<div><my-test-directive my-binding="isRendered"></my-test-directive></div>'; //i

    beforeEach(module('myApp'));

    beforeEach(inject(function($compile, $rootScope){
        scope = $rootScope.$new();        
        scope.isRendered = …
Run Code Online (Sandbox Code Playgroud)

unit-testing controller directive angularjs

5
推荐指数
1
解决办法
2882
查看次数

标签 统计

angularjs ×1

controller ×1

directive ×1

unit-testing ×1