Consider the following files/code:
Employee.ts
export class Employee {
id: string;
firstName: string;
lastName: string;
isEmployed: boolean = true;
isManager: boolean = false;
public static fullName = ():string => this.firstName + ' ' + this.lastName;
}
JobHistory.ts
import { Employee } from './employee';
export class JobHistory {
public propX: string;
public propY: string;
public getJobRecord = (e: Employee): any => {
// perform actions
// return job history
};
}
FormatterUtility.ts
import { Employee } from './employee';
import { JobHistory } from './jobHistory';
export class FormatterUtility {
public formatJobData(e: Employee) {
let jh: JobHistory = new JobHistory();
let jobData = jh.getJobRecord(e);
// modify data format
// return formatted data
}
}
I am working on creating a unit test for the formatJobData
method. However, I'm struggling to mock out JobHistory
and its properties.
Here's my progress so far:
FormatterUtility.spec.ts
describe('formatJobData', () => {
let fakeJobHistory = {
propX: '',
propY: ''
};
let sandbox;
let formatterUtil;
beforeEach(() => {
sandbox = sinon.createSandbox();
sandbox.stub(JobHistory, "prototype").value(fakeJobHistory);
});
afterEach(() => {
sandbox.restore();
});
it('should perform certain task', () => {
// create employee object
formatterUtil = new FormatterUtility();
var result = formatterUtil.formatJobData(employee);
console.log(result);
});
});
I've attempted using stubs and sandbox.replace
, but I can't seem to override the default properties or methods of JobHistory
.
Currently, the above code throws an exception in Phantom 2.1.1:
TypeError: Trying to alter enumerable attribute of unconfigurable property.
Furthermore, the console.log
output in my test displays all the default properties of JobHistory
rather than the changed values.
What could be the issue here? Am I missing something important?