javascript - Angular 用户界面/ Bootstrap : Testing a controller that uses a dialog

标签 javascript testing angularjs jasmine angular-ui

我有一个 Controller ,它使用来自 angular-ui/bootstrap 的对话框:

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

我对测试几乎是全新的,并且认为我可能需要测试两件事:

  1. 调用 $scope.editClientFeature() 时会打开一个对话框
  2. 在对话框关闭后成功调用 $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);
//        });
    });

});

现在的直接问题是我无法创建/打开对话框。我收到以下错误:

Chrome 25.0 (Mac) ClientFeatureController when editing a clientfeature encountered a declaration exception FAILED
    TypeError: Cannot call method 'dialog' of undefined

如果有人已经为类似的用例编写了测试并且可以为我提供一个示例,那会很棒,因为我很迷茫。

谢谢, 肖恩

最佳答案

直到现在,我一直在为同样的问题而苦苦挣扎,在浏览了 github 存储库之后,我找到了对话测试并将其用作起点:

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

关于javascript - Angular 用户界面/ Bootstrap : Testing a controller that uses a dialog,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15490150/

相关文章:

javascript - 带有标签和百分比的 d3 仪表图?

javascript - 光滑的 slider 。有没有办法检查 slider 滑动的位置?

node.js - 如何在 nodejs 中模拟请求和响应以测试中间件/ Controller ?

javascript - .on ("click") 不 append 到 HTML

javascript - 关键依赖: the request of a dependency is an expression on jQuery Form Validator plugin

javascript - 如何将 Controller 变量作为 AngularJS $resource 请求的参数传递?

javascript - 为什么 ng-repeat 在此 AngularJs 1.5.x 组件中不起作用

javascript - 即使输入无效,AngularJS 表单也会验证

c# - 使用 Selenium C# 的 Internet Explorer 自动化测试

scala - 在 scala 中模拟会生成 java.lang.NoSuchMethodException