2014-12-24 94 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 
    }) 
}) 

我已经在你的代码修改的东西,帮它可以帮助你