【问题标题】:How to spy on method in the scope that is not binded如何在未绑定的范围内监视方法
【发布时间】:2018-01-28 06:06:41
【问题描述】:

我正在尝试测试以下组件中的方法“removePlayer”在删除播放器时是否被调用:spyOn(scope, 'removePlayer').and.callThrough();和期望(scope.removePlayer).toHaveBeenCalled();但得到以下错误:“错误::removePlayer()方法不存在 用法:spyOn(, )" 请问如何实现呢?

.component("playerList", {
            templateUrl: "src/js/player/player.html",
            bindings: {
                list: '<'
            },
            controller: function() {
                this.removePlayer = function(index) {
                    console.log('Remove player with index : ', index);
                    if (index > -1) {
                        this.list.splice(index, 1);
                    }
                };
            }
        });

通过以下测试:

beforeEach(inject(function($rootScope, $compile) {
    scope = $rootScope.$new();
    scope.list = angular.copy(playersList);
    element = angular.element('<player-list list="list"></player-list>');
    element = $compile(element)(scope);

    scope.$apply();
}));

it('should render the text', function() {
    // spyOn(scope, 'removePlayer').and.callThrough();
    var title = element.find('h1');
    var deleteBtn = element[0].querySelector('button');
    expect(title.text()).toBe('This table contains ' + playersList.length + ' player.');
    deleteBtn.click();
    // expect(scope.removePlayer).toHaveBeenCalled();
    expect(title.text()).toBe('This table contains ' + (playersList.length - 1) + ' player.');
});

这是模板:

<div class="container">
    <h1 class- "player-table-title">This table contains {{ $ctrl.list.length }} players[![enter image description here][1]][1].</h1>
    <table class="table table-striped">
        <thead>
            <tr>
                <th>Name</th>
                <th>Position</th>
                <th>Team</th>
                <th>Actions</th>
            </tr>
        </thead>
        <tbody>
            <tr ng-repeat="player in $ctrl.list">
                <td>{{ player.name }}</td>
                <td>{{ player.position }}</td>
                <td>{{ player.team }}</td>
                <td>
                    <button type="button" class="btn btn-danger btn-sm" ng-click="$ctrl.removePlayer($index)">
                        <span class="glyphicon glyphicon-trash"></span>
                    </button>
                </td>
            </tr>
        </tbody>
    </table>
</div>

这是应用在浏览器中的外观:

【问题讨论】:

    标签: angularjs jasmine karma-jasmine angular-components


    【解决方案1】:

    我能够通过这样做来解决这个问题:element.controller('playerList');

     it('should render the text', function() {
        var component = element.controller('playerList');
        spyOn(component, 'removePlayer').and.callThrough();
        var title = element.find('h1');
        var deleteBtn = element[0].querySelector('button');
        expect(title.text()).toBe('Ce tableau contient ' + playersList.length + ' joueurs.');
        deleteBtn.click();
        expect(title.text()).toBe('Ce tableau contient ' + (playersList.length - 1) + ' joueurs.');
        expect(component.removePlayer).toHaveBeenCalled();
    });
    

    【讨论】:

      【解决方案2】:

      这个方法是控制器方法,不是作用域。顺便说一句,组件没有范围。

      尝试像这样获取组件的控制器(不要忘记注入$componentController

      controller = $componentController('playerList', {$scope: scope});

      然后在spyOn函数中将scope替换为controller

      spyOn(controller, 'removePlayer').and.callThrough();
      ....
      expect(controller.removePlayer).toHaveBeenCalled();
      

      【讨论】:

      • 对不起,不要在第一个答案中将范围替换为控制器。我编辑了它。
      • 感谢您的回复,我知道$componentController,但它对我不起作用,我的目的是在我单击删除btn时监视removePlayer方法,$componentController将无法处理这个案子对我来说:)
      • 我正在尝试做这样的事情:1 - spyOn(scope, 'removePlayer').and.callThrough(); 2- deleteBtn.click(); (这个调用removePlayer函数) 3- expect(scope.removePlayer).toHaveBeenCalled();
      • 你在控制器中定义的函数。你为什么要在示波器上窥探它?
      • $componentController 需要获取组件的控制器,然后调用方法。
      猜你喜欢
      • 1970-01-01
      • 2018-03-02
      • 1970-01-01
      • 2014-05-02
      • 2012-05-11
      • 1970-01-01
      • 2015-08-02
      • 2013-05-20
      • 1970-01-01
      相关资源
      最近更新 更多