gpt4 book ai didi

javascript - 使用 Jasmine 测试在 ngTable 中排序功能

转载 作者:行者123 更新时间:2023-11-29 18:12:14 24 4
gpt4 key购买 nike

我已经使用 ng-table 创建了一个应用程序,该应用程序运行良好,但我不知道如何为该排序和 getData 编写测试用例。

谁能告诉我一些测试该功能的解决方案

我的代码如下

Jasmine 测试用例

describe('Testing Controllers', function() {
describe('Testing WorkController Controller', function() {
var WorkController, $scope;

beforeEach(module('wsd'));

beforeEach(inject(function($controller, $rootScope) {
$scope = $rootScope.$new();
WorkController = $controller('WorkController', {
$rootScope: $rootScope,
$scope: $scope,
ngTableParams : ngTableParams,
$filter: $filter
});
}));

it('should tableParams when tableParams is called', function() {

});
});
});

workstation/main.js

angular.module('wsd.workstations', [])

.controller('WorkController', function($rootScope, $scope, $filter, ngTableParams)
{
$scope.myValues = [{name: "Moroni", age: 50},
{name: "Tiancum", age: 43},
{name: "Jacob", age: 27},
{name: "Nephi", age: 29},
{name: "Enos", age: 34},
{name: "Tiancum", age: 43},
{name: "Jacob", age: 27},
{name: "Nephi", age: 29},
{name: "Enos", age: 34},
{name: "Tiancum", age: 43},
{name: "Jacob", age: 27},
{name: "Nephi", age: 29},
{name: "Enos", age: 34},
{name: "Tiancum", age: 43},
{name: "Jacob", age: 27},
{name: "Nephi", age: 29},
{name: "Enos", age: 34}];

$scope.tableParams = new ngTableParams({
sorting: {
name: 'asc'
}
}, {
getData: function($defer, params) {
$scope.myValues = $filter('orderBy')($scope.myValues, params.orderBy());
$defer.resolve($scope.myValues);
}
});


$scope.searchDocuments = function()
{
// some other logic
};
});

更新 2

我这样做是为了测试,但是越来越

<failure type="">TypeError: &apos;undefined&apos; is not a function (evaluating &apos;$defer.resolve($scope.myValues)&apos;)

测试用例

    it('should check tableParams getData sorting', inject(function($q) {
var deferred = $q.defer();
var promise = deferred.promise;
promise.then(function(result) {
expect(result).toEqual(expectedResult);
});
$scope.myValues = [{name: "Moroni", age: 50},
{name: "Tiancum", age: 43},
{name: "Jacob", age: 27},
{name: "Nephi", age: 29},
{name: "Enos", age: 34},
{name: "Tiancum", age: 43},
{name: "Jacob", age: 27},
{name: "Nephi", age: 29},
{name: "Enos", age: 34},
{name: "Tiancum", age: 43},
{name: "Jacob", age: 27},
{name: "Nephi", age: 29},
{name: "Enos", age: 34},
{name: "Tiancum", age: 43},
{name: "Jacob", age: 27},
{name: "Nephi", age: 29},
{name: "Enos", age: 34}];

$scope.getData(promise, $scope.tableParams );
}));

最佳答案

你可以:

在 Controller 的 $scope 上声明 getData 函数,从而使其在您的测试中可用:

$scope.tableParams = new ngTableParams({
sorting: {
name: 'asc'
}
}, {
getData: $scope.getData
});

$scope.getData = function($defer, params) {
$scope.myValues = $filter('orderBy')($scope.myValues, params.orderBy());
$defer.resolve($scope.myValues);
}

在 beforeEach() 中注入(inject) $q。

使用 $q 创建一个 promise 对象。

为您的单元测试分配一些 $scope.myValues。

声明一个包含您预期结果的变量 - 即您排序的 $scope.myValues 数组。然后:

promise.then(function(result){
expect(result).toEqual(expectedResult);
}
$scope.getData(deferred , $scope.tableParams);

关于javascript - 使用 Jasmine 测试在 ngTable 中排序功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26301572/

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