【问题标题】:Testing Bootstrap modal in Angular controller在 Angular 控制器中测试 Bootstrap 模态
【发布时间】:2015-03-09 17:53:29
【问题描述】:

几天来,我一直试图找到一种方法来测试这个控制器部件,但一直卡住。现在我得到一个 ReferenceError: Can't find variable: $modal 但我已经注入了它,所以我不确定它为什么不起作用。我也知道我正在编写的这个测试并没有真正测试任何重要的东西,所以如果您对前进有任何建议,请告诉我。并感谢任何在整个控制器中帮助我编写代码的人

代码:

$scope.confirmDelete = function (account) {

        var modalInstance = $modal.open({
            templateUrl: '/app/accounts/views/_delete.html',
            controller: function (global, $scope, $modalInstance, account) {
                $scope.account = account;

                $scope.delete = function (account) {
                    global.setFormSubmitInProgress(true);
                    accountService.deleteAccount(global.activeOrganizationId, account.entityId).then(function () {
                        global.setFormSubmitInProgress(false);
                        $modalInstance.close();
                    },
                    function (errorData) {
                        global.setFormSubmitInProgress(false);
                    });

                };

                $scope.cancel = function () {
                    global.setFormSubmitInProgress(false);
                    $modalInstance.dismiss('cancel');
                };
            },
            resolve: {
                account: function () {
                    return account;
                }
            }
        });

测试:

describe("confirmDelete() function", function () {
        var controller, scope;

        // sets scope of controller before each test
        beforeEach(inject(function ($rootScope, _$modal_) {
            scope = $rootScope.$new();
            controller = $controller('AccountsController',
                {
                    $scope: scope,
                    $stateParams: mockStateParams,
                    $state: mockState,
                    // below: in order to call the $modal have it be defined and send on the mock modal? 
                    $modal: _$modal_,
                    //modalInstance: mockModalInstance,
                    global: mockGlobal,
                    accountService: mockAccountSrv
                });
        }));

        beforeEach(inject(function ($modal, $q) {
            spyOn($modal, 'open').and.returnValue({
                result: $q.defer().promise
            });
        }));

        it("make sure modal promise resolves", function () {
            scope.confirmDelete(mockAccountSrv.account);
            expect($modal.open).toHaveBeenCalled();
        });

    });

【问题讨论】:

标签: angularjs unit-testing jasmine


【解决方案1】:

您需要将 modal 设置为变量才能使用它。

describe("confirmDelete() function", function () {
     var controller, scope, $modal; //Initialize it here

//....

beforeEach(inject(function ($rootScope, _$modal_, $controller) {
      $modal = _$modal_; //Set it here

您还需要注入$controller 才能使用它。

Plnkr

【讨论】:

  • 我认为它可能是这样的,但如果我添加它,我会得到 TypeError: 'undefined' is not an object (evalating '$modal.open')
  • 你还需要注入$controller。检查我的演示。保持您的测试非常简单,首先修复它,修复所有参考问题、拼写错误等。然后改进测试并从那里开始我建议。
  • 我以为我永远不会经历那个。谢谢!现在我只需要停止犯新手错误。
猜你喜欢
  • 2014-08-13
  • 2013-03-07
  • 1970-01-01
  • 2015-10-15
  • 1970-01-01
  • 1970-01-01
  • 2014-01-16
  • 2017-02-24
  • 2014-11-22
相关资源
最近更新 更多