1

I have a small Angular 8 method, where I read a local file. However, it is possible, that the file does not exist, which is fine. Unfortunately, my Protractor e2e tests fail:

Entry({ level: SEVERE, message: 'http://localhost:4200/assets/details.txt - Failed to load resource: the server responded with a (...)

The method in question:

const headers = new HttpHeaders().set('Content-Type', 'text/plain; charset=utf-8');

this.http.get('assets/commit_details.txt', { headers, responseType: 'text' }).subscribe((data) => {
    (...)
}, (error) => {
    return throwError(`Commit Info: ${error.statusText}`);
});

In order for the tests to pass, I could of course adjust the Protractor settings, but that seems wrong.

How would I "allow" a missing file in my method?

PrimuS
  • 2,505
  • 6
  • 33
  • 66
  • first check if file exists or not, and write two separate tests for individual cases, one where file exists and other where it doesn't exist. It's always a good practice to check for resource before passing it to API. – Hemendra Oct 10 '19 at 07:45
  • use `try{} catch{}` and do nothing when error.Hope that solved your pblm – Madhan Raj Oct 10 '19 at 09:14

1 Answers1

0

You can use expectation return the error messaged and assert it.

expect('').toThrowError('')

Or just mute the error.

Infern0
  • 2,565
  • 1
  • 8
  • 21