Skip to content

Instantly share code, notes, and snippets.

@imdadul
Last active March 21, 2019 10:08
Show Gist options
  • Save imdadul/832e102acd0ebef9d9633653b4d5326e to your computer and use it in GitHub Desktop.
Save imdadul/832e102acd0ebef9d9633653b4d5326e to your computer and use it in GitHub Desktop.
Writing test for all functions.
/**
* @param firstName
* @param lastName
* @param address
* @constructor
*/
function Person(firstName, lastName, address) {
let self = this;
this.firstName = firstName;
this.lastName = lastName;
this.address = address;
/**
* @return {string}
*/
this.getAddress = function() {
return _.get(self.address, ['city']) + '(' + _.get(self.address, ['country']) + ')';
}
/**
* @return {string}
*/
this.getFullName = function() {
return self.firstName + ' ' + self.lastName;
}
/**
* @return {string}
*/
this.getInfo = function() {
return 'His name is ' + self.getFullName() + ' and lives in ' + self.getAddress();
};
}
// Person.spec.js
describe('Testing Person ', function() {
var person;
beforeEach(function() {
person = new Person('Imdadul', 'Huq', { country: 'Germany', city: 'Darmstadt' });
})
describe('Test of getInfo', function() {
beforeEach(function() {
spyOn(person, 'getFullName').and.callFake(function() {
return 'abraka-dabra';
});
spyOn(person, 'getAddress').and.callFake(function() {
return 'no-where';
});
});
expect(person.getInfo()).toEqual('His name is abraka-dabra and lives in no-where');
});
describe('Test of getFullName', function() {
expect(person.getFullName()).toEqual('Imdadul Huq');
});
describe('Test of getFullName', function() {
expect(person.getAddress()).toEqual('Darmstadt(Germany)');
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment