Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

381
Views
Jasmine test case error: spy error to have been called

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.

about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

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();
  });
about 4 years ago · Santiago Gelvez Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!