Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 单元测试角度指令,控制器运行两次,第一次范围错误_Angularjs_Unit Testing_Angularjs Directive_Jasmine - Fatal编程技术网

Angularjs 单元测试角度指令,控制器运行两次,第一次范围错误

Angularjs 单元测试角度指令,控制器运行两次,第一次范围错误,angularjs,unit-testing,angularjs-directive,jasmine,Angularjs,Unit Testing,Angularjs Directive,Jasmine,这是一个奇怪的 我有一个指令,基本上是这样的: function Toolbar() { return { scope:{ resultType: '=', }, restrict: 'E', bindToController: true, controller: 'ToolbarController', controllerAs:

这是一个奇怪的

我有一个指令,基本上是这样的:

    function Toolbar() {
    return {
        scope:{
            resultType: '=',              
        },
        restrict: 'E',
        bindToController: true,
        controller: 'ToolbarController',
        controllerAs: 'toolbar',
        templateUrl: 'toolbar.html'
    };
}
function ToolbarController($scope, $stateParams) {
    var that = this;
    that.key = $stateParams.key;

    switch (that.resultType){
        case 'r':
            that.flag = false;
            break;
        default :
            vm.flag = true;
            break;
    }
 }
it('should show the the correct flag', function() {
        scope = $rootScope.$new();
        scope.resultType = 'r';
        toolbarController = $controller('ToolbarController', {$scope: scope, 
    $stateParams: $stateParams});
            expect(toolbarController.flag).toBe(false);
        });
toolbarController = $controller('ToolbarController', {$scope: scope, 
$stateParams: $stateParams}, { resultType: 'r' });
我有一个控制器,看起来像这样:

    function Toolbar() {
    return {
        scope:{
            resultType: '=',              
        },
        restrict: 'E',
        bindToController: true,
        controller: 'ToolbarController',
        controllerAs: 'toolbar',
        templateUrl: 'toolbar.html'
    };
}
function ToolbarController($scope, $stateParams) {
    var that = this;
    that.key = $stateParams.key;

    switch (that.resultType){
        case 'r':
            that.flag = false;
            break;
        default :
            vm.flag = true;
            break;
    }
 }
it('should show the the correct flag', function() {
        scope = $rootScope.$new();
        scope.resultType = 'r';
        toolbarController = $controller('ToolbarController', {$scope: scope, 
    $stateParams: $stateParams});
            expect(toolbarController.flag).toBe(false);
        });
toolbarController = $controller('ToolbarController', {$scope: scope, 
$stateParams: $stateParams}, { resultType: 'r' });
我有一个测试,看起来像这样:

    function Toolbar() {
    return {
        scope:{
            resultType: '=',              
        },
        restrict: 'E',
        bindToController: true,
        controller: 'ToolbarController',
        controllerAs: 'toolbar',
        templateUrl: 'toolbar.html'
    };
}
function ToolbarController($scope, $stateParams) {
    var that = this;
    that.key = $stateParams.key;

    switch (that.resultType){
        case 'r':
            that.flag = false;
            break;
        default :
            vm.flag = true;
            break;
    }
 }
it('should show the the correct flag', function() {
        scope = $rootScope.$new();
        scope.resultType = 'r';
        toolbarController = $controller('ToolbarController', {$scope: scope, 
    $stateParams: $stateParams});
            expect(toolbarController.flag).toBe(false);
        });
toolbarController = $controller('ToolbarController', {$scope: scope, 
$stateParams: $stateParams}, { resultType: 'r' });
当我刚开始测试时,控制器似乎执行了两次。第一次响应类型未定义且测试失败时。第二次是对的,但到那时测试已经失败了

有人见过这个,知道问题出在哪里吗

谢谢,
James

要使用
bindToController
测试angularJS driective控制器,您必须将绑定项作为第三个参数传递给
$controller
,如下所示:

    function Toolbar() {
    return {
        scope:{
            resultType: '=',              
        },
        restrict: 'E',
        bindToController: true,
        controller: 'ToolbarController',
        controllerAs: 'toolbar',
        templateUrl: 'toolbar.html'
    };
}
function ToolbarController($scope, $stateParams) {
    var that = this;
    that.key = $stateParams.key;

    switch (that.resultType){
        case 'r':
            that.flag = false;
            break;
        default :
            vm.flag = true;
            break;
    }
 }
it('should show the the correct flag', function() {
        scope = $rootScope.$new();
        scope.resultType = 'r';
        toolbarController = $controller('ToolbarController', {$scope: scope, 
    $stateParams: $stateParams});
            expect(toolbarController.flag).toBe(false);
        });
toolbarController = $controller('ToolbarController', {$scope: scope, 
$stateParams: $stateParams}, { resultType: 'r' });
有关更多信息,请参见angularJS文档

发现了问题。有人添加了beforeach,在那里他们创建了工具栏的一个实例,并将其分配给一个实例变量,该实例变量使用与我的局部变量相同的名称

去杀人吧,谢谢


^不是字面意思,尽管我很受诱惑。

在您的测试中,您是在测试指令和控制器,还是只是测试碰巧被指令使用的控制器?可能与您的问题无关,但是在开关的默认部分,对vm.flag的引用不应该是.flag吗?这实际上是我尝试的第一件事,得到了相同的结果。为了验证,我在输出resultType值的switch语句上方添加了一个“throw”。第一次是未定义的,第二次是字母“r”。