As a beginner in Angular, I am diving into unit testing for the first time. Let's take a look at the module I'm working with:
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')
});
Additionally, there are two factories to consider:
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;
}])
My goal is to test the functions within the uri and portal factory.
Here is my attempt:
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);
})
})
However, upon running the test, I encountered the following error:
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)
...
It seems I am not initializing the uri factory correctly.
- How should I properly initialize the uri factory and test a function?
- How can I initialize the portal factory and test a function?