Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 角度ui/引导:测试使用对话框的控制器_Javascript_Testing_Angularjs_Jasmine_Angular Ui - Fatal编程技术网

Javascript 角度ui/引导:测试使用对话框的控制器

Javascript 角度ui/引导:测试使用对话框的控制器,javascript,testing,angularjs,jasmine,angular-ui,Javascript,Testing,Angularjs,Jasmine,Angular Ui,我有一个使用angular ui/引导程序对话框的控制器: function ClientFeatureController($dialog, $scope, ClientFeature, Country, FeatureService) { //Get list of client features for selected client (that is set in ClientController) $scope.clientFeatures =

我有一个使用angular ui/引导程序对话框的控制器:

   function ClientFeatureController($dialog, $scope, ClientFeature, Country, FeatureService) {

        //Get list of client features for selected client (that is set in ClientController)
        $scope.clientFeatures = ClientFeature.query({clientId: $scope.selected.client.id}, function () {
            console.log('getting clientfeatures for clientid: ' + $scope.selected.client.id);
            console.log($scope.clientFeatures);
        });

        //Selected ClientFeature
        $scope.selectedClientFeature = {};

        /**
         * Edit selected clientFeature.
         * @param clientFeature
         */
        $scope.editClientFeature = function (clientFeature) {
            //set selectedClientFeature for data binding
            $scope.selectedClientFeature = clientFeature;

            var dialogOpts = {
                templateUrl: 'partials/clients/dialogs/clientfeature-edit.html',
                controller: 'EditClientFeatureController',
                resolve: {selectedClientFeature: function () {
                    return clientFeature;
                } }
            };
            //open dialog box
            $dialog.dialog(dialogOpts).open().then(function (result) {
                if (result) {
                    $scope.selectedClientFeature = result;
                    $scope.selectedClientFeature.$save({clientId: $scope.selectedClientFeature.client.id}, function (data, headers) {
                        console.log('saved.');
                    }, null);
                }
            });
        };
    });
我几乎完全不熟悉测试,我想也许我需要测试两件事:

  • 调用$scope.editClientFeature()时打开一个对话框
  • 对话框关闭并返回“结果”后,成功调用$save
  • 我的测试现在看起来非常糟糕:

    describe('ClientFeatureController', function () {
        var scope, $dialog, provider;
    
        beforeEach(function () {
                inject(function ($controller, $httpBackend, $rootScope, _$dialog_) {
                scope = $rootScope;
                $dialog = _$dialog_;
    
                //mock client
                scope.selected = {};
                scope.selected.client = {
                    id: 23805
                };
    
                $httpBackend.whenGET('http://localhost:3001/client/' + scope.selected.client.id + '/clientfeatures').respond(mockClientFeatures);
                $controller('ClientFeatureController', {$scope: scope});
                $httpBackend.flush();
            });
        });
    
    
        it('should inject dialog service from angular-ui-bootstrap module', function () {
            expect($dialog).toBeDefined();
            console.log($dialog); //{}
        });
    
        var dialog;
    
        var createDialog = function (opts) {
            dialog = $dialog.dialog(opts);
        };
    
        describe('when editing a clientfeature', function () {
            createDialog({});
            console.log(dialog); //undefined
    //        var res;
    //        var d;
    //        beforeEach(function () {
    //            var dialogOpts = {
    //                template: '<div>dummy template</div>'
    //            };
    //            console.log(dialog);
    //            d = $dialog.dialog(dialogOpts);
    //            d.open();
    //        });
    //
    //        it('should open a dialog when editing a client feature', function () {
    //            expect(d.isOpen()).toBe(true);
    //        });
        });
    
    });
    
    $dialog.messageBox = function (title, msg, btns) {
        return {
            open: function () {
                return {
                     then: function (callback) {
                          callback('ok'); // 'ok' will be set to param result
                     }
                 }
            }
        }
     };
    
    如果有人已经为一个类似的用例编写了一个测试,并且可以为我提供一个例子,那就太好了,因为我已经迷路了

    谢谢,
    肖恩

    就我个人而言,我试图嘲弄所有的服务。如果ui引导项目没有提供$dialog模拟,那么您应该在那里打开一个bug通知单,并要求他们提供一个。然而,创建一个也很容易


    模拟服务应该有假的方法,这些方法除了返回承诺之外什么都不做。它还应该为您提供一个刷新所有异步方法的方法,以便更容易进行同步测试。

    我也更喜欢一个合适的模拟。当它不可用时,我会修补该服务

    要测试这一点:

    $dialog.messageBox(title, msg, btns)
       .open()
       .then(function (result) {
           if (result == 'ok') {
                // block executed if user click OK
           }
    });
    
    您可以按如下方式修补$dialog:

    describe('ClientFeatureController', function () {
        var scope, $dialog, provider;
    
        beforeEach(function () {
                inject(function ($controller, $httpBackend, $rootScope, _$dialog_) {
                scope = $rootScope;
                $dialog = _$dialog_;
    
                //mock client
                scope.selected = {};
                scope.selected.client = {
                    id: 23805
                };
    
                $httpBackend.whenGET('http://localhost:3001/client/' + scope.selected.client.id + '/clientfeatures').respond(mockClientFeatures);
                $controller('ClientFeatureController', {$scope: scope});
                $httpBackend.flush();
            });
        });
    
    
        it('should inject dialog service from angular-ui-bootstrap module', function () {
            expect($dialog).toBeDefined();
            console.log($dialog); //{}
        });
    
        var dialog;
    
        var createDialog = function (opts) {
            dialog = $dialog.dialog(opts);
        };
    
        describe('when editing a clientfeature', function () {
            createDialog({});
            console.log(dialog); //undefined
    //        var res;
    //        var d;
    //        beforeEach(function () {
    //            var dialogOpts = {
    //                template: '<div>dummy template</div>'
    //            };
    //            console.log(dialog);
    //            d = $dialog.dialog(dialogOpts);
    //            d.open();
    //        });
    //
    //        it('should open a dialog when editing a client feature', function () {
    //            expect(d.isOpen()).toBe(true);
    //        });
        });
    
    });
    
    $dialog.messageBox = function (title, msg, btns) {
        return {
            open: function () {
                return {
                     then: function (callback) {
                          callback('ok'); // 'ok' will be set to param result
                     }
                 }
            }
        }
     };
    

    我一直在努力解决同样的问题,直到现在,在浏览github repo之后,我找到了对话测试,并以此作为起点:

    var $dialog,$scope,$httpBackend;
      beforeEach(module('ui.bootstrap.dialog'));
      beforeEach(function(){
        inject(function (_$dialog_, _$httpBackend_, $controller){
          $dialog = _$dialog_;
          $httpBackend = _$httpBackend_;
          $httpBackend.expectGET('/appServer/list')
            .respond([{
                id:1,
                name:'test1'
              },
              {
                id:2,
                name:'test2'
              },
              {
                id:3,
                name:'test3'
              }]);
    
    
          //setup controller scope
          scope = {};
          ServerCtrl = $controller('ServerCtrl', {
            $scope: scope,
            $dialog:$dialog
          });
        });
      });
    

    我发现写我自己的对话模拟是最清楚的。下面是一个模拟对话框以模拟选择“是”的示例

    测试中的代码 测验
    我从你的帖子中意识到,我忘了在beforeach中包含“ui.bootstrap”模块。非常感谢。
        describe('when deleting a house', function () {
    
            var fakeDialog = {
                open: function()
                {
                    return {
                        then: function(callback) {
                            callback("yes");
                        }
                    };
                }
            };
    
            beforeEach(inject(function($dialog) {
                spyOn($dialog, 'messageBox').andReturn(fakeDialog);
            }));
    
            it('should call the remove method on the houseRepository', function () {
                scope.remove({id: 99});
                expect(houseRepository.remove).toHaveBeenCalledWith(99);
            });
    
            // etc
        });