【发布时间】:2014-09-18 18:55:19
【问题描述】:
我的项目正在使用 AngularJS + Kendo-UI。我正在尝试测试我的一个使用 Kendo-UI 网格的控制器:
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) 的示例,但我更喜欢使用角度库来做到这一点。
【问题讨论】:
标签: javascript angularjs unit-testing kendo-ui kendo-grid