2016-06-18 6 views
0

私はui-bootstrapモーダル・ウィンドウを使用していますが、そのモーダル・ウィンドウを起動するメソッドをテストしようとしています。私のコントローラ:同じコントローラ内の角度モーダル・インスタンスのテスト

app.controller('AddProductController', ['$scope', 'ProductsService', '$uibModal', function ($scope, ProductsService, $uibModal) { 
     $scope.product = {}; 
     $scope.searchCategories = function() { 
      ProductsService.getRootCategories().then(function (data) { 
       $scope.categories = data.data; 
      }); 
      $scope.modalInstance = $uibModal.open({ 
       animation: $scope.animationsEnabled, 
       templateUrl: 'categoryContent.html', 
       controller: 'AddProductController', 
       scope: $scope 
      }); 
      $scope.modalInstance.result.then(function (category) { 
       $scope.searchCategory = null; 
       $scope.product.category = category; 
      }, function() { 
      }); 
     }; 
     $scope.ok = function(){ 
      $scope.modalInstance.close($scope.product.category); 
     }; 
     $scope.cancel = function(){ 
      $scope.modalInstance.dismiss(); 
    }]); 

そして、私のテスト:

describe("Products Controller", function() { 
beforeEach(function() { 
    module('productsController'); 
}); 

beforeEach(function() { 
    var ProductsService, createController, scope, rootScope, 

    module(function ($provide) { 
     $provide.value('ProductsService', { 
      getRootCategories: function() { 
       return { 
        then: function (callback) { 
         return callback({data: {name: 'category1'}}); 
        } 
       }; 
      }, 
     }); 

     $provide.value('$uibModal', { 
      open : function(){ 
       return { 
        then: function (callback) { 
         return callback({data: {name: 'category1'}}); 
        } 
       }; 
      } 
     }); 

     return null; 
    }); 
}); 
describe('AddProductController', function() { 
    beforeEach(function() { 
     inject(function ($controller, _$rootScope_, _ProductsService_) { 
      rootScope = _$rootScope_; 
      scope = _$rootScope_.$new(); 
      ProductsService = _ProductsService_; 
      createController = function() { 
       return $controller("AddProductController", { 
        $scope: scope, 
       }); 
      }; 
     }); 
    }); 
    it('calling searchCategories should make $scope.categories to be defined', function() { 

     createController(); 
     expect(scope.categories).not.toBeDefined(); 
     scope.searchCategories(); 
     expect(scope.categories).toBeDefined(); 
    }); 
}); 

});

私のすべてのテストは合格しましたが、ここではTypeError:$ scope.modalInstance.resultが定義されていません。 手がかりはありますか?

答えて

1

モックモーダルでresultを定義していないようです。次のように試してみてください:

$provide.value('$uibModal', { 
    open: function() { 
     return { 
      result : { 
       then: function (callback) { 
        return callback({ data: { name: 'category1' } }); 
       } 
      } 
     }; 
    } 
}); 
関連する問題