2016-09-09 5 views
0

SinonJsでスコープを共有する方法を教えてください。ここでSinonJSのユニットテスト中にangular-ecmascript6コントローラでスコープを共有する方法

はあなたの参照のための私の抜粋です:

// set up the mock 
    beforeEach(() => { 

    MockHomeApiService = function() { 
     return { 
     requestForEarlyAccess:()=>{ 
      console.log("homeApiService.requestForEarlyAccess"); 
     } 
     } 
    }; 

    MockTranslate = function() { 
     return sinon.stub().resolves("true"); 
    }; 

    }); 


    // set up the angular mocks 
    beforeEach(() => { 
     // provide the 
     module(($provide) => { 
     $provide.service('homeApiService', MockHomeApiService); 
     $provide.service('$translate', MockTranslate); 
     }); 

     // you can surround any injected dependencies with _ _ and // the injector will throw them away, it's just to  // differentiate between variables that you might want to  // use for tests. 
     inject((_$q_, _$timeout_, _homeApiService_, _$translate_) => { 

      $q = _$q_ 
      // having $timeout is often useful for testing that 
      // promises have been fulfilled 
      $timeout = _$timeout_; 

      // set up sinon-as-promised, otherwise it won't work! 
      sinonAsPromised($q); 


      homeApiService = _homeApiService_; 
      $translate = _$translate_ 

     }); 
    }); 


// set up your controller using mocked dependencies 
    beforeEach(() => { 
    controller = new ApplyController(homeApiService, userAlertsService, $log, $translate); 
    }); 

答えて

0
inject((_$q_, _$timeout_, _homeApiService_, _$translate_, _$rootScope_) => { 

     $q = _$q_ 
     // having $timeout is often useful for testing that 
     // promises have been fulfilled 
     $timeout = _$timeout_; 

     // set up sinon-as-promised, otherwise it won't work! 
     sinonAsPromised($q); 


     homeApiService = _homeApiService_; 
     $translate = _$translate_; 
     $rootScope = _$rootScope_; 

    }); 

とテスト・スイートまたは各前 -

$scope = $rootScope.$new(true); 
+0

あなたの方法を使用することにより、それは私がコントローラをしたい代わりにこのの空白$範囲を示し、すべてのメソッドと変数を利用できる範囲 –

+0

作成したスコープをコントローラに渡す必要があります。テストで変異スコープを使用する場合は、すべてのテストの前にinitコントローラを設定する必要があります。しかし、良い方法 - コントローラとスコープを全く使用せず、コントローラをシンタックスとして使用し、これにバインドします。 http://stackoverflow.com/questions/14202767/testing-scope-in​​-angularjs-controller-with-dependency-on-filterを参照してください。 –

関連する問題