Skip to content

Instantly share code, notes, and snippets.

@imdadul
Created March 21, 2019 10:06
Show Gist options
  • Save imdadul/5a3588ffc8ae061af42bed870c93d6d7 to your computer and use it in GitHub Desktop.
Save imdadul/5a3588ffc8ae061af42bed870c93d6d7 to your computer and use it in GitHub Desktop.
How mocking can be dangerous sometime, missing the integration between functions.
function Person(firstName, lastName, address) {
let self = this;
this.firstName = firstName;
this.lastName = lastName;
this.address = address;
/**
* @return {string}
*/
this.getInfo = function() {
return {
address: address,
tax: new TaxCalculator(self).getTaxBy() // Must parameter to this getTaxBy function, which is wrong in this line!
}
};
}
// tax-calculator.service / module.
function TaxCalculator(param) {
let person = param;
this.getTaxBy = function(year) {
if(!year || person.address.country) {
return new Error('Required info is missing')
}
}
}
describe('Testing Person', function() {
var person;
var firstName = 'Imdadul';
var lastName = 'Huq';
var address = { country: 'Germany', city: 'Darmstadt' };
beforeEach(function() {
person = new Person(firstName, lastName, address);
});
describe('Test of getInfo', function() {
beforeEach(function() {
// For example let's mock a function of the class.
spyOn(TaxCalculator, 'getTaxBy').and.callFake(function() {
return 100;
});
});
expect(person.getInfo()).toEqual({ // This test will pass! even though undefined variable has been passed!
address:address,
tax:100
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment