In my AngularJS application, I have defined an object model like this:
.factory('Watermark', function () {
// Constructor, with a class name
// Assumption: the backend provides us with a topic or not!
function Watermark(content, title, author, topic) {
this.content = content;
this.title = title;
this.author = author;
if(topic) {
this.topic = topic;
}
}
// Example Public method, assigned to prototype
Watermark.prototype.getDocumentType = function () {
return this.content;
};
return Watermark;
});
Now, I want to test this using Jasmine. I want to verify that when I create a new instance of an object, its constructor
has the value of the function I used (in this case, Watermark). My current test is failing, as shown below...
describe('Service: Watermark', function () {
// Load the module / app
beforeEach(module('MyAppName'));
var Watermark;
beforeEach(inject(function (_Watermark_){
Watermark = _Watermark_;
setTimeout(function(done) {
done();
}, 1);
}));
it('should be defined', function () {
expect(Watermark).toBeTruthy();
});
describe('when defining a watermark with content "Book"', function () {
it('should be an instance of "Watermark"', function () {
var waterMark = new Watermark('Book','When boyfriends aint so super','Mary Jane','Doomed Romance');
expect(waterMark instanceof Watermark).toBeTruthy();
});
//THIS FAILS!!!
xit('should have a constructor property "Watermark"', function () {
var waterMark = new Watermark('Book','When boyfriends aint so super','Mary Jane','Doomed Romance');
console.log(waterMark.constructor);
expect(waterMark.constructor).contains('Watermark');
});
it('should contain a content property with value "book"', function () {
var waterMark = new Watermark('Book','When boyfriends aint so super','Mary Jane','Doomed Romance');
expect(waterMark).toEqual(jasmine.objectContaining({
content: 'Book'
}));
});
it('should contain a title property with value "When boyfriends aint so super"', function () {
var waterMark = new Watermark('Book','When boyfriends aint so super','Mary Jane','Doomed Romance');
expect(waterMark).toEqual(jasmine.objectContaining({
title: 'When boyfriends aint so super'
}));
});
it('should contain an author property with "Mary Jane"', function () {
var waterMark = new Watermark('Book','When boyfriends aint so super','Mary Jane','Doomed Romance');
expect(waterMark).toEqual(jasmine.objectContaining({
author: 'Mary Jane'
}));
});
it('should contain a topic property with "Doomed Romance"', function () {
var waterMark = new Watermark('Book','When boyfriends aint so super','Mary Jane','Doomed Romance');
expect(waterMark).toEqual(jasmine.objectContaining({
topic: 'Doomed Romance'
}));
});
});
describe('when defining a watermark with content "journal"', function () {
it('should not contain a topic property', function () {
var waterMark = new Watermark('Journal','My pretty lousey life as SpiderMan', 'Peter Parker');
expect(Object.keys(waterMark)).not.toContain('topic');
});
});
});
I am getting an error TypeError: undefined is not a constructor and I'm unsure of what I'm doing wrong here?
Is there a way to reduce the repetition of declaring the new instance within the it('....
Thanks in advance.