2017-06-19 28 views
1

我想使用茉莉花的beforeAll而不是beforeEach,但angular.mock.module和angular.mock.inject函數不工作在beforeAll,而他們在之前工作。angular.mock.module和angular.mock.inject函數不工作在茉莉花的beforeAll

這是我的測試。 相同的代碼在beforeEach方法中工作。

describe("This is a test", function() { 
    beforeAll(module("app")); 

    var vm; 
    beforeAll(function() { 
     angular.mock.module(function ($provide) { 
      $provide.factory("dataService", ["$q", function ($q) { 
       return { 
        getSomeDataById: function() { return $q.resolve({ }); } 
       }; 
      }]); 
     }); 

     angular.mock.inject(function (_$controller_,dataService) { 
      vm = _$controller_("TestController", 
      { 
       dataService: dataService 
      }); 
     }); 
    }); 
}); 

回答

0

我正面臨着類似的問題,並使用module.sharedInjector()呼叫解決這對我來說:

describe("This is a test", function() { 

    // Call SharedInjector before any call that might invoke the injector 
    angular.mock.module.sharedInjector(); 

    beforeAll(module("app")); 

    var vm; 
    beforeAll(function() { 
     angular.mock.module(function ($provide) { 
      $provide.factory("dataService", ["$q", function ($q) { 
       return { 
        getSomeDataById: function() { return $q.resolve({ }); } 
       }; 
      }]); 
     }); 

     angular.mock.inject(function (_$controller_,dataService) { 
      vm = _$controller_("TestController", 
      { 
       dataService: dataService 
      }); 
     }); 
    }); 
});