My application is making an http call to my backend immediately on page load. My e2e test is failing because there is no backend running in my ci pipelines.
I have tried using the rxjs catchError piped operator on the http call
I have tried wrapping the whole http call in a try / except block
I am still getting the error showing up in the dev console (which is causing the e2e test to fail)
I am wondering how I can provide a mock HttpClient
to the protractor tests?
Actual Http call
return this.http.get<any>( url ).subscribe(...)
(this.http
is an instance of angular's HttpClient
)
spec file:
import { AppPage } from './app.po';
import { browser, logging } from 'protractor';
describe( 'workspace-project App', () => {
let page: AppPage;
beforeEach( () => {
page = new AppPage();
} );
it( 'should display login page', () => {
page.navigateTo();
expect( page.getLoginTitleText() ).toEqual( 'Welcome to
App
Click below to sign in with Google' );
} );
afterEach( async () => {
// Assert that there are no errors emitted from the browser
const logs = await browser.manage().logs().get( logging.Type.BROWSER );
expect( logs ).not.toContain( jasmine.objectContaining( {
level: logging.Level.SEVERE,
} as logging.Entry ) );
} );
} );
protractor's page object file (app.po
)
import { browser, by, element } from 'protractor';
export class AppPage {
public navigateTo() {
return browser.get( browser.baseUrl ) as Promise<any>;
}
public getLoginTitleText() {
return element( by.css( 'app-root app-login div.login-wrapper form.login section.title' ) )
.getText() as Promise<string>;
}
}
error in dev console:
question from:
https://stackoverflow.com/questions/65832462/mock-angular-httpclient-in-protractor-e2e-test 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…