使用Jasmine和业力测试工厂方法。得到错误:[$ injector:unpr]

时间:2017-04-17 07:23:49

标签: angularjs jasmine karma-runner

如何使用Jasmine和karma测试此工厂方法getItemList

  

我收到错误:[$ injector:unpr] http://errors.angularjs

这是我的代码。

(function() {

    angular.module('riskCanvasApp').factory('itemsService', itemsService);

    itemsService.$inject = [ '$http', '$q','$compile', 'UrlService',
        'accountDetailsMainService', 'sharedService', 'authenticationSvc' ];

    function itemsService($http, $q, $compile, urlService, accountDetailsMainService,
                          sharedService, authenticationSvc) {


        function getItemList() {
           // Code here

        }

我的单元测试代码是

'use strict';
(function() {

describe('itemsService Spec', function() {

    var _authenticationSvc;
    var _$http;
    var _$q;
    var _$compile;
    var _urlService;
    var myFactory;

    beforeEach(module('riskCanvasApp'));

    beforeEach(inject(function($http, $q, $compile, urlService, authenticationSvc) {
        debugger
        _$http = $http;
        _$q = $q;
        _$compile = $compile;
        _urlService = urlService;
        _authenticationSvc = authenticationSvc;

        var $injector = angular.injector(['riskCanvasApp']);
        myFactory = $injector.get('itemsService');

        var deferred = $q.defer();
    }));

    it('should call AuthService', function() {
        debugger
        var data = myFactory.getItemList();
        //
    });

});
}());

1 个答案:

答案 0 :(得分:0)

我得到了一个解决方案

 'use strict';
   (function() {
    //1. Describe the test
    describe('myserv', function () {
        var itemsService;
        var authenticationSvc;
        var urlService;
        var $q;
        var $scope;
        var httpBackend;

        //2. Load the module and inject dependencies
        beforeEach(function () {
            module('riskCanvasApp');
            inject( function($injector){
                itemsService = $injector.get('itemsService');
                authenticationSvc = $injector.get('authenticationSvc');
                urlService = $injector.get('UrlService');
                $q = $injector.get('$q');
                $scope = $injector.get('$rootScope').$new();
                httpBackend = $injector.get('$httpBackend');
            });
        });

        //3. make sure no expectations were missed in your tests.
        afterEach(function () {
            httpBackend.verifyNoOutstandingExpectation();
            httpBackend.verifyNoOutstandingRequest();
        });

        //4. write the test cases
        it('ServiceTestSpec', function () {
            // expect to make sure this is defined and valid function.
            expect(itemsService.itemList).toBeDefined();
            expect(itemsService.itemList).toEqual(jasmine.any(Function)); // Service to get Account type, Account sub type, Risk lavels, Assignment status, Sub types, Due dates

            // expect to make sure this is defined and valid function.
            expect(itemsService.createFilter).toBeDefined();
            expect(itemsService.createFilter).toEqual(jasmine.any(Function)); // Create filter

            // expect to make sure this is defined and valid function.
            expect(itemsService.updateFilter).toBeDefined();
            expect(itemsService.updateFilter).toEqual(jasmine.any(Function)); // Edit filter

            // expect to make sure this is defined and valid function.
            expect(itemsService.setDefaultFilter).toBeDefined();
            expect(itemsService.setDefaultFilter).toEqual(jasmine.any(Function)); // Set default filter

            // expect to make sure this is defined and valid function.
            expect(itemsService.deleteFilter).toBeDefined();
            expect(itemsService.deleteFilter).toEqual(jasmine.any(Function)); // Delete filter

            // expect to make sure this is defined and valid function.
            expect(itemsService.getFilters).toBeDefined();
            expect(itemsService.getFilters).toEqual(jasmine.any(Function)); // List filter

            // Handle error that requests logged in user
            httpBackend.whenGET('app/login/LoginView.html').respond({});
            httpBackend.flush();

            // variable in this scope for you to test.
            var returnData = {};

            // expectGET to make sure this is called once.
            httpBackend.expectGET("sampleJson/itemsObject.json").respond(returnData);

            // make the call.
            var returnedPromise = itemsService.itemList();

            // set up a handler for the response, that will put the result into a variable in this scope for you to test.
            var result;
            returnedPromise.then(function (response) {
                result = response.data;
            });

            // flush the backend to "execute" the request to do the expectedGET assertion.
            httpBackend.flush();

            // check the result.
            expect(result).toEqual(returnData);
        });

    });
}());