我對Angular相當陌生,並且已經回顧了Stack Overflow上所有類似的相關問題,但都沒有幫助過我。我相信我已經正確設置了一切,但在嘗試將服務注入到單元測試時仍然出現「未知提供者」錯誤。我在下面列出了我的代碼 - 希望有人可以發現一個明顯的錯誤!在向角單元測試注入服務時獲取未知提供者錯誤
我定義我模塊在一個單獨的的.js文件是這樣的:
angular.module('dashboard.services', []);
angular.module('dashboard.controllers', []);
這裏是我定義了一個稱爲EventingService服務(具有爲簡潔移除邏輯):
angular.module('dashboard.services').factory('EventingService', [function() {
//Service logic here
}]);
這裏是我使用EventingService的控制器(這一切在運行時都可以正常工作):
angular.module('dashboard.controllers')
.controller('Browse', ['$scope', 'EventingService', function ($scope, eventing) {
//Controller logic here
}]);
這裏是我的單元測試 - 它在那裏我試圖注入,當我運行單元測試會導致一個錯誤的EventingService行:
describe('Browse Controller Tests.', function() {
beforeEach(function() {
module('dashboard.services');
module('dashboard.controllers');
});
var controller, scope, eventingService;
beforeEach(inject(function ($controller, $rootScope, EventingService) {
scope = $rootScope.$new();
eventingService = EventingService
controller = $controller('Browse', {
$scope: scope,
eventing: eventingService
});
}));
it('Expect True to be True', function() {
expect(true).toBe(true);
});
});
當我運行測試我得到這個錯誤:
Error: Unknown provider: EventingServiceProvider <- EventingService
我已確保我的茉莉花specrunner.html文件擁有所有必要的源文件(這是一個Asp.Net MVC項目):
<!-- Include source files here... -->
@Scripts.Render("~/bundles/jquery")
<script type="text/javascript" src="@Url.Content("~/Scripts/angular.js")"></script>
<script type="text/javascript" src="@Url.Content("~/Scripts/angular-mocks.js")"></script>
<script type="text/javascript" src="@Url.Content("~/App/scripts/app.js")"></script> <!-- Angular modules defined in here -->
<script type="text/javascript" src="@Url.Content("~/App/scripts/services/eventing.js")"></script> <!-- My Eventing service defined here -->
<script type="text/javascript" src="@Url.Content("~/App/scripts/controllers/browse.js")"></script> <!-- My Browse controller defined here -->
<!-- Include spec files here... -->
<script type="text/javascript" src="@Url.Content("~/App/tests/browse.js")"></script> <!-- The actual unit test here -->
我不僅僅是C一個不明白爲什麼Angular會拋出這個錯誤,抱怨我的EventingService。我的控制器在運行時工作正常 - 只是當我試圖測試它時,我得到一個錯誤,所以我很好奇我是否已經嘲笑/注入了某些東西。
在測試上的角度幫助是垃圾,所以我目前難住 - 任何人可以給予的幫助或建議將非常感激。謝謝。
在這個時候在角度單元測試的新的,總是同意'在測試的角度幫助是垃圾,所以我被困在目前' – Michel 2015-12-08 19:51:54