注射工厂时的噶玛茉莉花错误

时间:2016-12-13 12:42:58

标签: angularjs unit-testing jasmine karma-jasmine angularjs-factory

我想使用karma-jasmine测试我的工厂文件。但我注入服务/工厂的线路显示错误。

我想测试http调用是否发生以及服务的所有后果。

angular demo.js

'use strict';
angular.module('rootMod', [''])
    .factory('demoService', function($http) {
      console.log("okk");
        var demoService = {};

        demoService.getdata = function(url) {
console.log("okk");
            return $http.get(url).then(function(response) {
                return response;
            });
        };

        return demoService;
    });

karma-jasmine demo-spec.js

'use strict';
describe("demo_Spec", function() {
            var demoService;

            describe('Testing demo Service', function() {
                var demoService;

                beforeEach(function() {
                    module('rootMod');
                    inject(function($injector) {
                        demoService = $injector.get('demoService');
                    });
                });

                describe('Checking for the existance demoService', function() {
                    it("Testing for service existance demoService", function() {
                        expect(demoService).toBeDefined();
                    });

                });
            });
          });

控制台错误

13 12 2016 18:09:39.588:WARN [karma]: No captured browser, open http://localhost
:9876/
13 12 2016 18:09:39.602:INFO [karma]: Karma v1.3.0 server started at http://loca
lhost:9876/
13 12 2016 18:09:39.603:INFO [launcher]: Launching browser PhantomJS with unlimi
ted concurrency
13 12 2016 18:09:39.609:INFO [launcher]: Starting browser PhantomJS
13 12 2016 18:09:41.208:INFO [PhantomJS 2.1.1 (Windows 7 0.0.0)]: Connected on s
ocket /#1yLeaZaK9QW2Qb8TAAAA with id 244265
PhantomJS 2.1.1 (Windows 7 0.0.0) demo_Spec Testing demo Service Checking for th
e existance demoService Testing for service existance demoService FAILED
        bower_components/angular/angular.js:4757:53
        forEach@bower_components/angular/angular.js:357:24
        loadModules@bower_components/angular/angular.js:4717:12
        createInjector@bower_components/angular/angular.js:4639:30
        WorkFn@bower_components/angular-mocks/angular-mocks.js:3141:60
        inject@bower_components/angular-mocks/angular-mocks.js:3121:46
        test/demo-spec.js:10:27
        loaded@http://localhost:9876/context.js:151:17
        Expected undefined to be defined.
        test/demo-spec.js:17:56
        loaded@http://localhost:9876/context.js:151:17
PhantomJS 2.1.1 (Windows 7 0.0.0): Executed 1 of 1 (1 FAILED) (0 secs / 0.013 se
PhantomJS 2.1.1 (Windows 7 0.0.0): Executed 1 of 1 (1 FAILED) ERROR (0.005 secs

0 个答案:

没有答案