2014-12-24 129 views
0

我真的很陌生,並嘗試單元測試的第一次。 我有以下模塊:角茉莉花,試圖測試一個工廠功能

var app = angular. 
module('webportal', 
[ 
    'vr.directives.slider', 
    'angular-flexslider', 
    'LocalStorageModule', 
    'multi-select', 
    'djds4rce.angular-socialshare' 
]).run(function ($FB) {//facebook share...should we move this somewhere else? 
    $FB.init('xxxxx') 
}); 

和下面的兩個工廠:

angular.module('webportal').factory('uri', function() { 

    var uri = {}; 

    uri.base = ''; 

    uri.setBase = function (base) { 
     uri.base = base; 
    }; 


    uri.getBase = function() { 
     return uri.base; 
    } 

    return uri; 
}); 

app.factory('portal', ['uri', function (uri) { 
    var portal = {}; 
    portal.getLink = function (id) { 
     return uri.getBase() + langHalf + '/property/' + id; 
    }; 
    return portal; 
}]) 

我試圖單元測試URI和門戶工廠內的功能。

這裏是我的嘗試:

describe('Unit: Factory Test', function() { 

    var uri; 

    beforeEach(function() { 
     angular.mock.module('vr.directives.slider', []); 
     angular.mock.module('angular-flexslider', []); 
     angular.mock.module('LocalStorageModule', []); 
     angular.mock.module('multi-select', []); 
     angular.mock.module('djds4rce.angular-socialshare', []); 

     module('webportal', [ 
      'vr.directives.slider', 
      'angular-flexslider', 
      'LocalStorageModule', 
      'multi-select', 
      'djds4rce.angular-socialshare' 
     ]); 

     beforeEach(module('uri')); 


    }); 


    it("baseSettingTest", function() { 
     var uri = new uri(); 
     //var uri = new uri; 
     var baseSettingTest = 'testing base'; 
     uri.setBase(baseSettingTest); 

     expect(uri.getBase()).toEqual(baseSettingTest); 

    }) 

}) 

因此,當我運行它,它失敗,出現以下錯誤:

FAILED Unit: Factory Test baseSettingTest 
TypeError: undefined is not a function 
    at Object.<anonymous> (http://localhost:9876/base/tests/portaltestjs/portal.test.js:50:19) 
    at attemptSync (http://localhost:9876/base/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:1759:24) 
    at QueueRunner.run (http://localhost:9876/base/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:1747:9) 
    at QueueRunner.execute (http://localhost:9876/base/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:1733:10) 
    at Spec.Env.queueRunnerFactory (http://localhost:9876/base/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:569:35) 
    at Spec.execute (http://localhost:9876/base/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:318:10) 
    at Object.fn (http://localhost:9876/base/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:2072:43) 
    at attemptAsync (http://localhost:9876/base/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:1789:24) 
    at QueueRunner.run (http://localhost:9876/base/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:1745:16) 
    at QueueRunner.execute (http://localhost:9876/base/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:1733:10) 
    window.__karma__.result 
    specDone 
    dispatch 
    (anonymous function) 
    specResultCallback  
    complete 
    clearStack  
    QueueRunner.run 
    QueueRunner.execute 
    Env.queueRunnerFactory  
    Spec.execute 
    fn  
    attemptAsync 
    QueueRunner.run 
    QueueRunner.execute 
    Env.queueRunnerFactory  
    Suite.execute 
    fn  
    attemptAsync 
    QueueRunner.run 
    QueueRunner.execute 
    Env.queueRunnerFactory  
    Suite.execute 
    allFns.push.fn  
    attemptAsync 
    QueueRunner.run 
    QueueRunner.execute 
    Env.queueRunnerFactory  
    Env.execute 
    env.executeFiltered 
    (anonymous function) 
    window.__karma__.loaded 
    (anonymous function) 
Skipped 0 tests 

所以很明顯我沒有intializing URI正確。

  1. 如何初始化uri工廠?並嘗試測試一個函數?
  2. 如何初始化門戶工廠?並嘗試測試函數?

回答

0

你需要注入的服務,然後你可以調用該服務的方法,這裏是我做到了,我ng-boilerplate項目:

beforeEach(module('ng-boilerplate')); 

    var NameService; 
    beforeEach(inject(function (_NameService_) { 
    NameService = _NameService_; 
    })); 

    describe('#formatName()', function() { 
    it('should title case a given string', function() { 
     expect(NameService.formatName('IAN')).to.equal('Ian'); 
    // ...etc... 
0

必須添加指向同你的「URI的第一件事「模板

describe('Unit: Factory Test', function() { 

    var uri; 

    beforeEach(function() { 
     angular.mock.module('vr.directives.slider', []); 
     angular.mock.module('angular-flexslider', []); 
     angular.mock.module('LocalStorageModule', []); 
     angular.mock.module('multi-select', []); 
     angular.mock.module('djds4rce.angular-socialshare', []); 

     module('webportal', [ 
      'vr.directives.slider', 
      'angular-flexslider', 
      'LocalStorageModule', 
      'multi-select', 
      'djds4rce.angular-socialshare' 
     ]); 

     //beforeEach(module('uri')); please comment out this line 
     beforeEach(module('webportal')) //you should load module instead of load your factory 

    }); 

    // Use below block code to inject your factory into uri variable 
    beforeEach(inject(function (_$injector_) { 
     uri = _$injector_.get('uri'); //Inject uri factory into uri variable 
    })); 

    // For now your uri factory available for testing 

    it("baseSettingTest", function() { 
     spyOn(uri, 'setBase'); //spy on you setBase function 

     var baseSettingTest = 'testing base'; 
     uri.setBase(baseSettingTest);  

     expect(uri.getBase).toHaveBeenCalled(); //Make sure getBase function was called 
    }) 
}) 

我已經在你的代碼修改的東西,幫它可以幫助你