繁体   English   中英

单元测试使用Kendo Grid / Datasource的Angular Controller

[英]Unit testing Angular Controller that uses Kendo Grid/Datasource

我的项目是使用AngularJS + Kendo-UI。 我正在尝试测试一个使用Kendo-UI Grid的控制器:

angular.module('myApp')('DevicesCtrl', function ($scope) {

    $scope.gridOptions = {
        dataSource: {
            transport: {
                read: {
                    url: "/devices",
                    dataType: "json"
                }
            },
            change: function(){
                var view = this.view();
                $scope.devices = [];

                $.each(view, function(i, v) {
                    $scope.devices.push({id: v.id, description: v.name, status: v.status  == 0 ? "failure" : "success"});
                });
                $scope.$apply();
            }
        },
        columns: [
            {
                field: "name",
                title: "Name",
                width: 250,
                template: function (item) {
                    var itemStatus = item.status == 0 ? 'failure' : 'success';
                    return '<div label size="small" operation="' + itemStatus + '" label="' + item.name + '"></div>';
                }
            },
            {
                field: "status",
                title: "Status"
            }
        ]
    };
});

当我编写单元测试时,我预计将调用GET请求:

describe('deviceCtrl', function () {

    var scope, deviceCtrl, httpBackend, timeout;

    beforeEach(module("myApp"));
    beforeEach(module('src/modules/devices/views/device.html'));

    beforeEach(inject(function ($controller, $rootScope, $httpBackend, $timeout, $state) {
        scope = $rootScope.$new();

        httpBackend = $httpBackend;
        timeout = $timeout;

        httpBackend.expectGET('languages/en_US.json').respond({});

        deviceCtrl = $controller("DeviceCtrl", {
            $scope: scope
        });

        $state.go("devices");
        timeout.flush()

    }));

    it('should load the switch list', function () {

        httpBackend.expectGET("/devices").respond(
            [{"id":"1","name":"name 1","status":"1"},
            {"id":"2","name":"name 2","status":"2"}]
        );

        httpBackend.flush();

    });

});

但是期望永远不会满足,没有提出要求。 所以我的问题是:有没有办法让Kendo Grid / Datasource进行这个调用,所以我可以模拟它?

我看到了一些关于如何使用Mockjax( http://www.telerik.com/forums/best-practice-mocking-with-datasource )做的示例,但我更喜欢使用角度库来做到这一点。

经过大量的研究,我可以找到一些方法来测试使用Kendo Datasource的控制器。

Kendo有自己的方式来进行Ajax调用以获取数据,并且不使用常规的Angular $ http来做到这一点,因此使用Angular工具(角度模拟)进行测试有点棘手。 我们来看看选项:

1 - 使用常规的Angular方式进行Ajax调用。

Kendo让我们改变了获取数据的方式,所以不要这样做:

dataSource: new kendo.data.DataSource({
            transport: {
                read: {
                    url: "/devices,
                    dataType: "json"
                }
            },
            change: function(){
                var view = this.view();
                $scope.devices = [];

                $.each(view, function(i, v) {
                    $scope.devices.push({id: v.id, description: v.name, status: v.status  == 0 ? "failure" : "success"});
                });
                $scope.$apply();
            }
        });

我们将改为:

dataSource: new kendo.data.DataSource({
            transport: {
                read: function(options){
                    $http.get("/devices").then(function(response){
                        options.success(response.data);
                        $scope.devices = [];
                        response.data.forEach(function(v){
                            $scope.devices.push({id: v.id, description: v.name, status: v.status  == 0 ? "failure" : "success"});
                        });
                    });
                }
            }
        });

然后我们可以使用常规$ httpBackend.expectGET(url)来模拟Ajax调用。 我个人喜欢这种方法,因为我们有更多的控制权。 Ps。:使用函数内部的变量“options”,我们可以访问Grid过滤器,排序和分页值。

2 - 模拟Kendo Ajax Call。

通过这种方式,我们在Controller中几乎没有任何改变,唯一需要改变的是使用新的kendo.data.DataSource({})创建一个新的数据源而不是仅传递选项。 这需要,因为我们在测试用例中调用了read函数。 我尝试了不同的方法来模拟这个Ajax请求,但我唯一可以使它工作的是使用一个名为Jasmine-Ajax的工具。 在测试用例中,我们会写一些类似于:

var request = jasmine.Ajax.requests.mostRecent();
request.response(MockData);

我希望这可以帮助其他人。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM