热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

如何向茉莉花注入服务-Howtoinjectaservicetojasmine

IhavethefollowingtestcaseMeetingCtrlSpec.js我有以下测试用例MeetingCtrlSpec.jsdescribe(ViewMeeting

I have the following test case MeetingCtrlSpec.js

我有以下测试用例MeetingCtrlSpec.js

describe('ViewMeetingCtrl', function () {
        var $rootScope, scope, $controller   ;

       beforeEach(angular.mock.module('MyApp'));

       beforeEach(inject(function ($rootScope, $controller  ) {
            scope = $rootScope.$new();
            $controller('ViewMeetingCtrl', {
            $scope: scope,
           }); 
        }));

        it('should change greeting value if name value is changed', function () {
            //some assertion
        });
    });

ViewMeetingCtrl.js

(function () {
    'use strict';
    angular.module('MyApp').controller('ViewMeetingCtrl', ViewMeetingCtrl);

    ViewMeetingCtrl.$inject = ['$scope', '$state', '$http', '$translate', 'notificationService', 'meetingService', '$modal', 'meeting', 'attachmentService'];

    function ViewMeetingCtrl($scope, $state, $http, $translate, notificationService, meetingService, $modal, meeting, attachmentService) {
        $scope.meeting = meeting;                    
        //more code goes here
    }
})();

this meeting comes from the app.routes.js file

这次会议来自app.routes.js文件

.state('company.meeting', {
                abstract: true,
                url: '/meetings/:meetingId',
                template: '',
                resolve: {
                    meeting: function(meetingService, $stateParams){
                        return meetingService
                                .getMeeting($stateParams.meetingId)
                                .then(function(response){
                                    return response.data;
                                });
                    }
                },
            })

My problem is regarding the injection of meeting in this ctrl . I am not sure how to do inject that in my test case. I did like the following .

我的问题是关于在此ctrl中注入会议。我不知道如何在我的测试用例中注入它。我确实喜欢以下内容。

describe('ViewMeetingCtrl', function () {
            var $rootScope, scope, $controller , meeting   ;

           beforeEach(angular.mock.module('MyApp'));

           beforeEach(inject(function ($rootScope, $controller , meeting     ) {
                scope = $rootScope.$new();
                $controller('ViewMeetingCtrl', {
                $scope: scope,
                meeting : meeting   
               }); 
            }));

            it('should change greeting value if name value is changed', function () {
                //some assertion
            });
        });

... and i got this error Error: [$injector:unpr] Unknown provider: meetingProvider <- meeting

...我收到此错误错误:[$ injector:unpr]未知提供者:meetingProvider <- 会议

How do i inject meeting dependency to my test case . ?

如何将会议依赖注入我的测试用例。 ?

1 个解决方案

#1


1  

Meeting is not a service, but an object that is injected when route is resolve. In you test case you should explicitly create the meeting dummy object.

会议不是服务,而是在路由解析时注入的对象。在测试用例中,您应该显式创建会议虚拟对象。

 beforeEach(inject(function ($rootScope, $controller,$q ) {
                scope = $rootScope.$new();

                $controller('ViewMeetingCtrl', {
                $scope: scope,
                meeting : {}  //your custom object
               }); 
            })); 

Remember you are testing the controller in your test not the route resolution injection.

请记住,您在测试中测试控制器而不是路径分辨率注入。


推荐阅读
author-avatar
绝非韩版560
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有