单元测试 – 在AngularJS单元测试中模拟$modal

前端之家收集整理的这篇文章主要介绍了单元测试 – 在AngularJS单元测试中模拟$modal前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我为一个控制器写一个单元测试,触发一个$ modal,并使用promise返回执行一些逻辑。我可以测试父控制器触发$模态,但我不能为我的生活搞清楚如何模拟一个成功的承诺。

我尝试了很多方法包括使用$ q和$ scope。$ apply()强制promise的解决方案。然而,最近我已经得到了一些类似于最后的答案在this SO post;

我看到这个问题几次与“老”$对话框模态。
我不能找到很多关于如何做到与“新的”$对话框模态。

一些指针将被赞赏。

为了说明这个问题我使用example provided in the UI Bootstrap docs,有一些小的编辑。

控制器(主和模态)

  1. 'use strict';
  2.  
  3. angular.module('angularUiModalApp')
  4. .controller('MainCtrl',function($scope,$modal,$log) {
  5. $scope.items = ['item1','item2','item3'];
  6.  
  7. $scope.open = function() {
  8.  
  9. $scope.modalInstance = $modal.open({
  10. templateUrl: 'myModalContent.html',controller: 'ModalInstanceCtrl',resolve: {
  11. items: function() {
  12. return $scope.items;
  13. }
  14. }
  15. });
  16.  
  17. $scope.modalInstance.result.then(function(selectedItem) {
  18. $scope.selected = selectedItem;
  19. },function() {
  20. $log.info('Modal dismissed at: ' + new Date());
  21. });
  22. };
  23. })
  24. .controller('ModalInstanceCtrl',$modalInstance,items) {
  25. $scope.items = items;
  26. $scope.selected = {
  27. item: $scope.items[0]
  28. };
  29.  
  30. $scope.ok = function() {
  31. $modalInstance.close($scope.selected.item);
  32. };
  33.  
  34. $scope.cancel = function() {
  35. $modalInstance.dismiss('cancel');
  36. };
  37. });

视图(main.html)

  1. <div ng-controller="MainCtrl">
  2. <script type="text/ng-template" id="myModalContent.html">
  3. <div class="modal-header">
  4. <h3>I is a modal!</h3>
  5. </div>
  6. <div class="modal-body">
  7. <ul>
  8. <li ng-repeat="item in items">
  9. <a ng-click="selected.item = item">{{ item }}</a>
  10. </li>
  11. </ul>
  12. Selected: <b>{{ selected.item }}</b>
  13. </div>
  14. <div class="modal-footer">
  15. <button class="btn btn-primary" ng-click="ok()">OK</button>
  16. <button class="btn btn-warning" ng-click="cancel()">Cancel</button>
  17. </div>
  18. </script>
  19.  
  20. <button class="btn btn-default" ng-click="open()">Open me!</button>
  21. <div ng-show="selected">Selection from a modal: {{ selected }}</div>
  22. </div>

考试

  1. 'use strict';
  2.  
  3. describe('Controller: MainCtrl',function() {
  4.  
  5. // load the controller's module
  6. beforeEach(module('angularUiModalApp'));
  7.  
  8. var MainCtrl,scope;
  9.  
  10. var fakeModal = {
  11. open: function() {
  12. return {
  13. result: {
  14. then: function(callback) {
  15. callback("item1");
  16. }
  17. }
  18. };
  19. }
  20. };
  21.  
  22. beforeEach(inject(function($modal) {
  23. spyOn($modal,'open').andReturn(fakeModal);
  24. }));
  25.  
  26.  
  27. // Initialize the controller and a mock scope
  28. beforeEach(inject(function($controller,$rootScope,_$modal_) {
  29. scope = $rootScope.$new();
  30. MainCtrl = $controller('MainCtrl',{
  31. $scope: scope,$modal: _$modal_
  32. });
  33. }));
  34.  
  35. it('should show success when modal login returns success response',function() {
  36. expect(scope.items).toEqual(['item1','item3']);
  37.  
  38. // Mock out the modal closing,resolving with a selected item,say 1
  39. scope.open(); // Open the modal
  40. scope.modalInstance.close('item1');
  41. expect(scope.selected).toEqual('item1');
  42. // No dice (scope.selected) is not defined according to Jasmine.
  43. });
  44. });
当你在beforeEach中监听$ modal.open函数时,
  1. spyOn($modal,'open').andReturn(fakeModal);
  2.  
  3. or
  4.  
  5. spyOn($modal,'open').and.returnValue(fakeModal); //For Jasmine 2.0+

你需要返回一个模拟$ modal.open通常返回的模拟,而不是$ modal的模拟,它不包括一个打开的功能,你在你的fakeModal模拟。假模态必须有一个结果对象,它包含一个then函数来存储回调(单击OK或Cancel按钮时调用)。它还需要一个关闭功能(模拟确定按钮点击模态)和一个关闭功能(模拟取消按钮点击模态)。 close和dismiss函数调用调用必要的回调函数

将fakeModal更改为以下内容,单元测试将通过:

  1. var fakeModal = {
  2. result: {
  3. then: function(confirmCallback,cancelCallback) {
  4. //Store the callbacks for later when the user clicks on the OK or Cancel button of the dialog
  5. this.confirmCallBack = confirmCallback;
  6. this.cancelCallback = cancelCallback;
  7. }
  8. },close: function( item ) {
  9. //The user clicked OK on the modal dialog,call the stored confirm callback with the selected item
  10. this.result.confirmCallBack( item );
  11. },dismiss: function( type ) {
  12. //The user clicked cancel on the modal dialog,call the stored cancel callback
  13. this.result.cancelCallback( type );
  14. }
  15. };

此外,您可以通过在取消处理程序中添加一个要测试的属性来测试取消对话框,在本例中为$ scope.canceled:

  1. $scope.modalInstance.result.then(function (selectedItem) {
  2. $scope.selected = selectedItem;
  3. },function () {
  4. $scope.canceled = true; //Mark the modal as canceled
  5. $log.info('Modal dismissed at: ' + new Date());
  6. });

一旦设置了取消标志,单元测试将如下所示:

  1. it("should cancel the dialog when dismiss is called,and $scope.canceled should be true",function () {
  2. expect( scope.canceled ).toBeUndefined();
  3.  
  4. scope.open(); // Open the modal
  5. scope.modalInstance.dismiss( "cancel" ); //Call dismiss (simulating clicking the cancel button on the modal)
  6. expect( scope.canceled ).toBe( true );
  7. });

猜你在找的Angularjs相关文章