gpt4 book ai didi

angularjs - 在 Jasmine 单元测试中调用 ng-grid 上的 selectRow

转载 作者:行者123 更新时间:2023-12-02 01:52:43 25 4
gpt4 key购买 nike

我使用 ng-grid 对 Angular Controller 进行了 jasmine 测试。我想打电话selectRow(1, true);在 Angular 网格上,但是当我这样做时,我得到错误 TypeError: Object #<Object> has no method 'selectRow'

我有一个 Angular Controller ,它像这样设置一个 ng-grid:

    $scope.myGridOptions = {
data : 'myData',
multiSelect: false,
selectedItems: $scope.mySelectedRow,
afterSelectionChange: function(data) {;
if($scope.mySelectedRow === undefined){
//do somthing
}else{
//do somthing else
}
}
};

然后我进行了 jasmine 单元测试,我使用注入(inject)函数实例化了我的 Controller ,设置了 scope对象等于 Controller $scope .然后我有一堆不涉及网格的测试,它们都运行并通过。

但是我想测试选择处理程序逻辑,所以我是这样测试的:

    it('selecting a row in the table does stuff.', function() {
//controller created in before each
rootScope.$digest();//needed to get the controller to initialize
scope.myGridOptions.$selectRow(1, true);
expect(scope.resultOfDoingSomthing).toBe("done");
});

如果需要更多代码示例,请在评论中告诉我。对于背景,我使用 maven jasime:bdd运行测试,所以我的 Jasmine 配置就是这样设置的。我认为它没有任何问题,因为我有很多其他 angular-jasmine 单元测试都按预期工作。包含 ng-grid javascript。我也尝试添加 module("ngGrid")beforeEach没有变化。我也尝试添加 beforeEach(module("myMod",['ng-grid']));但这爆炸了 $injector错误。

最佳答案

好的其他人跟随。我所做的是直接测试 afterSelectionChange 而不是尝试通过调用 selectRow 来测试它。为了使其有效,我必须重构:

    afterSelectionChange: function(data) {;
if($scope.mySelectedRow === undefined){
//do somthing
}else{
//do somthing else
}
}

    afterSelectionChange: function(rowItem, event) {;
if(rowItem.entity === undefined){
//do somthing
}else{
//do somthing with row rowItem
}
}

如您所见,它使用传递给它的数据。

关于angularjs - 在 Jasmine 单元测试中调用 ng-grid 上的 selectRow,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21827765/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com