在ngTable排序功能使用茉莉花测试 [英] sorting feature in ngTable using Jasmine Testing

查看:267
本文介绍了在ngTable排序功能使用茉莉花测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经用NG-表的应用程序,该应用程序工作正常,但我don'nt知道如何编写一个测试案例的整理与的getData。

谁能告诉我一些解决方案,用于测试功能

我的code为如下

给出

茉莉花测试用例

 描述(测试控制器,函数(){
    描述(测试WorkController控制器',函数(){
        VAR WorkController,$范围;        beforeEach(模块('WSD'));        beforeEach(注(函数($控制器,$ rootScope){
            $范围= $ rootScope $新的()。
            WorkController = $控制器('WorkController',{
                $ rootScope:$ rootScope,
                $范围:$范围,
                ngTableParams:ngTableParams,
                $过滤:过滤$
            });
        }));        它('当tableParams叫应tableParams',函数(){        });
    });
});

工作站/ main.js

  angular.module('wsd.workstations',[]).controller('WorkController',函数($ rootScope,$范围,$过滤器,ngTableParams)
{
   $ scope.myValues​​ = [{名:莫罗尼,年龄:50},
                {名:Tiancum,年龄:43},
                {名:雅各布,年龄:27},
                {名:尼,年龄:29},
                {名:挪,年龄:34},
                {名:Tiancum,年龄:43},
                {名:雅各布,年龄:27},
                {名:尼,年龄:29},
                {名:挪,年龄:34},
                {名:Tiancum,年龄:43},
                {名:雅各布,年龄:27},
                {名:尼,年龄:29},
                {名:挪,年龄:34},
                {名:Tiancum,年龄:43},
                {名:雅各布,年龄:27},
                {名:尼,年龄:29},
                {名:挪,年龄:34}];    $ scope.tableParams =新ngTableParams({
        排序:{
            名称:ASC
        }
    },{
        的getData:函数($延迟,则params){
            $ scope.myValues​​ = $滤波器('ORDERBY')($ scope.myValues​​,params.orderBy());
            $ defer.resolve($ scope.myValues​​);
        }
    });
    $ scope.searchDocuments =功能()
    {
        //一些其他的逻辑
    };
});

更新2

我也做过这样的测试,但得到

 <故障类型=>类型错误:&放大器;者;不确定和放大器;者;不是函数(评价&放大器;者; $ defer.resolve($ scope.myValues​​)及')的

测试用例

 它('应检查tableParams的getData排序',注入(函数($ Q){
            变种推迟= $ q.defer();
            VAR承诺= deferred.promise;
            promise.then(功能(结果){
               预期(结果).toEqual(expectedResult);
            });
             $ scope.myValues​​ = [{名:莫罗尼,年龄:50},
                {名:Tiancum,年龄:43},
                {名:雅各布,年龄:27},
                {名:尼,年龄:29},
                {名:挪,年龄:34},
                {名:Tiancum,年龄:43},
                {名:雅各布,年龄:27},
                {名:尼,年龄:29},
                {名:挪,年龄:34},
                {名:Tiancum,年龄:43},
                {名:雅各布,年龄:27},
                {名:尼,年龄:29},
                {名:挪,年龄:34},
                {名:Tiancum,年龄:43},
                {名:雅各布,年龄:27},
                {名:尼,年龄:29},
                {名:挪,年龄:34}];            $ scope.getData(诺言,$ scope.tableParams);
}));


解决方案

您可以:

声明控制器的$范围GetData函数,从而使其在测试可供选择:

  $ scope.tableParams =新ngTableParams({
        排序:{
            名称:ASC
        }
    },{
        的getData:$ scope.getData
    });$ scope.getData =功能($延迟,则params){
            $ scope.myValues​​ = $滤波器('ORDERBY')($ scope.myValues​​,params.orderBy());
            $ defer.resolve($ scope.myValues​​);
        }

注资$ Q在beforeEach()。

使用$ Q创建一个承诺对象。

分配约$ scope.myValues​​你的单元测试。

声明包含您预期的结果的变量 - 那就是你整理$ scope.myValues​​阵列。然后:

  promise.then(功能(结果){
    预期(结果).toEqual(expectedResult);
}
$ scope.getData(延期,$ scope.tableParams);

I have created an application using ng-table , the application is working fine but i don'nt know how to write a test case for that sorting and getData.

Can anyone please tell me some solution for testing that functionality

My code is as given below

jasmine test case

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
    };
});

Update 2

I have done like this for testing, but getting

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

test cases

    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 );
}));

解决方案

You could:

Declare getData function on controller's $scope, thus making it available in your test:

$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);
        }

Inject $q in beforeEach().

Create a promise object using $q.

Assign some $scope.myValues for your unit test.

Declare a variable containing your expected result - that is your sorted $scope.myValues array. Then:

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

这篇关于在ngTable排序功能使用茉莉花测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆