我真的很有角度并且第一次尝试进行单元测试。 我有以下模块:
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
很明显,我没有正确地使用uri。
答案 0 :(得分: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...
答案 1 :(得分:0)
首先,你必须为你的&#39; uri&#39;添加参考。模板
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
})
})
我修改了代码中的内容,帮助它帮助您