I have the following code that needs to be tested in Jasmine
sampleMethod() {
if (this.sampleObject) {
// business logic
} else {
console.error('sampleObject not initialized');
}
}
I had referred to Spying on console.error() with Jasmine to add jasmine test to monitor console.error.
it('should check for console.error', () => {
spyOn(console, 'error');
component.sampleMethod();
expect(console.error).toHaveBeenCalled();
});
However, I'm getting the following error:
Error: Expected spy error to have been called.
SampleObject is undefined. I've also tried adding spyOn to beforeEach. Still, I'm getting the error.
Can you try this to ensure that sampleObject is undefined and try assigning the spy to a variable?
it('should check for console.error', () => {
const errorSpy = spyOn(console, 'error');
component.sampleObject = undefined;
component.sampleMethod();
expect(errorSpy).toHaveBeenCalled();
});