1

함수 내에서 하나의 $ mdDialog를 호출했습니다. $ mdDialog ok를 단위 테스트하고 사례를 취소하고 싶습니다.

아래 내용이 내 컨트롤러 코드입니다 (app.controller.js). 다음

(function() { 
    'use strict'; 

    app.controller('AppCtrl', AppCtrl); 

    AppCtrl.$inject = ['$scope', '$mdDialog']; 

    function AppCtrl($scope, $mdDialog) { 

     $scope.saveEntry = function (ev) { 
      var confirm = $mdDialog.prompt() 
       .title('Save Entry') 
       .textContent('If you want, you can add a description to explain what you changed.') 
       .placeholder('Version Description') 
       .ariaLabel('Version Description') 
       .initialValue('') 
       .targetEvent(ev) 
       .ok('Save') 
       .cancel('Cancel'); 

      $mdDialog.show(confirm).then(function (result) { 
       $scope.status = true; 
      }, function() { 
       $scope.status = false; 
      }); 
     }; 
    } 

})(); 

사양 코드 (을 app.controller.spec.js) :

나는 다음과 같은 오류 받고 있어요 위의 코드를 실행
describe('Unit test AppController: mdDialog', function() { 
    var $controller, $mdDialog; 
    beforeEach(function() { 
     module('App'); 
     inject(function (_$controller_, _$mdDialog_) { 
      $controller = _$controller_; 
      $mdDialog = _$mdDialog_; 
     }); 
    }); 

    it(': Opened', function() { 
     var $scope = {}; 
     var controller = $controller('AppCtrl', { $scope: $scope }); 

     var $mdDialogOpened = false; 
     $mdDialog.show = jasmine.createSpy().and.callFake(function() { 
      $mdDialogOpened = true; 
     }); 

     $scope.saveEntry(); 
     $scope.$digest(); 

     expect($mdDialog.show).toHaveBeenCalled; 
     expect($mdDialogOpened).toBe.true; 
    }); 
}); 

: 형식 오류를 : 정의되지 않은 'then'속성을 읽을 수 없습니다.

이 GitHub 문제 https://github.com/angular/material/issues/1482을 언급했습니다. 하지만 문제는 당신이 mdDialog$의 한 버전을 주입하고, 다른 하나에서 테스트하려고하는 것입니다 사전

답변

3

내 문제에 대한

감사 솔루션을받지 못했습니다.
이 같은 것을 시도 할 수 : 매우 유사

describe('Unit test AppController: mdDialog', function() { 
    var ctrl, mdDialog, scope; 

    beforeEach(function() { 
     module('App'); 
     inject(function ($rootScope, $controller, $mdDialog) { 
     scope = $rootScope.$new(); 
     mdDialog = $mdDialog; //keep the reference, for later testing. 

     spyOn(mdDialog, 'show'); 
     mdDialog.show.and.callFake(function() { 
     return { 
      then: function (callBack) { 
         callBack(true); //return the value to be assigned. 
        } 
     } 
     });  

     ctrl = $controller('AppCtrl',{$scope:scope, $mdDialog:mdDialog}); //Inject the dependency 

     }); 
    }); 

    it(': Opened', function() { 
     scope.saveEntry(); //exercise the method. 
     scope.$digest(); 

     expect(mdDialog.show).toHaveBeenCalled(); 
     expect(scope.status).toBe(true); 
    }); 
}); 

뭔가가 작동합니다.
이 도움이 되었기를 바랍니다.

+0

답변 해 주셔서 감사합니다. 나는 당신이 $ rootScope.new()에서 $를 놓쳤다 고 생각한다. 대답을 업데이트 해주십시오. –

+0

완료 : D, thanks! – Hosar

관련 문제