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正確。
- 如何初始化uri工廠?並嘗試測試一個函數?
- 如何初始化門戶工廠?並嘗試測試函數?