2017-08-11 98 views
0

我需要一些幫助才能使此測試運行。我花了很多時間在上面。Karma/Jasmine如何在angularjs工廠上調用方法

所以我卡住了,需要一些幫助?

我只是有一個工廠方法,有一個方法,我不斷從[獲取] [無法獲取屬性'method1'的未定義或空引用]錯誤。

(function() { 
    'use strict'; 
    var first = function() { 
     var method1 = function() { 
      return 'yes'; 
     }; 
     return { 
      method1: method1 
     }; 
    }; 
    angular.module('app').factory('first', [first]); 
})(); 

這裏是我嘗試調用method1。

describe('Test Suite', function() { 

    describe('Factory Test', function() { 
     beforeEach(function() { 
      module('app'); 
     }); 

     var service; 

     //beforeEach(inject(function (_first_) { 
     // service = _first_; 
     //  console.log(JSON.stringify(_first_, null, 4)); 
     //})); 

     beforeEach(inject(function ($injector) { 
      service = $injector.get('first'); 
      console.log(JSON.stringify(service, null, 4)); 
     })); 

     //beforeEach(function() { 
     // angular.mock.inject(function ($injector) { 
     //  service = $injector.get('first'); 
     // }) 
     //}); 

     describe('calling method1', function() { 
      var output = service.method1(); 
      expect(output).toBe('yes'); 
     }); 
    }); 
}); 

和噶

IE 11.0.0 (Windows 10 0.0.0) Test Suite Factory Test calling method1 encountered a declaration exception FAILED 
TypeError: Unable to get property 'method1' of undefined or null reference 
    at Anonymous function (tests/firstTest.spec.js:31:13) 
    at Anonymous function (tests/firstTest.spec.js:30:9) 
    at Anonymous function (tests/firstTest.spec.js:4:5) 
IE 11.0.0 (Windows 10 0.0.0): Executed 1 of 1 (1 FAILED) (0 secs/0.135 secs) 
IE 11.0.0 (Windows 10 0.0.0): Executed 1 of 1 (1 FAILED) ERROR (0.14 secs/0.135 secs) 

回答

0

哎呀我的輸出,愚蠢的我。

我忘了添加它(「」,function(){})的東西。

謝謝