如何向茉莉花注入服务

How to inject a service to jasmine

我有以下测试用例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
    }
})();

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

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

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

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
            });
        });

...我得到了这个错误 Error: [$injector:unpr] Unknown provider: meetingProvider <- meeting

如何将 meeting 依赖项注入我的测试用例。 ?

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

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

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

请记住,您在测试中测试的是控制器,而不是路由解析注入。