测试 – 将服务注入角度单元测试时获取未知提供程序错误

前端之家收集整理的这篇文章主要介绍了测试 – 将服务注入角度单元测试时获取未知提供程序错误前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我是相当新的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 -->

我只是不能fathom为什么Angular抛出这个错误抱怨我的EventingService。我的控制器在运行时工作正常 – 这只是当我试图测试它,我得到一个错误,所以我很好奇是否我搞砸了一些嘲笑/注入。

Angular帮助测试是垃圾,所以我目前的困惑 – 任何帮助或建议任何人都可以给予非常赞赏。谢谢。

我刚刚遇到这个和解决它通过切换到获取服务使用$ injector显式:
var EventingService,$rootScope;
beforeEach(inject(function($injector) {
  EventingService = $injector.get('EventingService');
  $rootScope = $injector.get('$rootScope');
}));

我希望我能告诉你为什么这个工作,为什么简单

beforeEach(inject(function(EventingService) {   ....  }));

不,但我没有时间调查内部。总是最好使用一种编码风格,坚持下去。

这种风格是更好的,因为您在测试中使用的变量的名称是服务的正确名称。但它有点冗长。

还有另一个角魔法功能,使用奇怪的变量名称,如$ rootScope,但我不喜欢这个黑客的外观。

请注意,大多数时候人们得到​​这个错误,因为他们没有包括模块:

beforeEach(module('capsuling'));
beforeEach(module('capsuling.capsules.services'));
原文链接:https://www.f2er.com/angularjs/145564.html

猜你在找的Angularjs相关文章