i am trying to unit test my login page using jasmine and karma but am always having this error 'Error: Expected one matching request for criteria "Match URL: https://beta-lp.biodymanager.com/fr/api/login_check ", found none.' when i want to make sure that the method login is calling the post function that calls ' https://beta-lp.biodymanager.com/fr/ ' + SERVICE_LOGIN
this is my spec file
describe('LoginPage', () => {
let component: LoginPage;
let fixture: ComponentFixture<LoginPage>;
let loginBtn: HTMLButtonElement;
let eye : HTMLIonIconElement;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ LoginPage ],
schemas: [CUSTOM_ELEMENTS_SCHEMA],
imports: [
BrowserModule,
FormsModule ,
RouterModule.forRoot([]),
RouterTestingModule.withRoutes([{ path: 'tabs/dashboard', component: LoginPage },{ path: 'tabs/home', component: LoginPage }]),
ReactiveFormsModule,
TranslateModule.forRoot(),
IonicModule,
IonicStorageModule.forRoot(),
HttpClientTestingModule,
] ,
providers : [LoginPage,InAppBrowser, HTTP, Device, Globalization, AngularDelegate, CommonService ,
AuthService,ModalController,Network,RestApiService,{
provide: APP_BASE_HREF,
useValue: '/'
}]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(LoginPage);
component = fixture.componentInstance;
loginBtn = fixture.debugElement.nativeElement.querySelector('ion-button');
});
it('should create', () => {
expect(component).toBeTruthy();
});
describe('login method', ()=> {
const value : IUser =({
email : 'emnas@gmail.com',
password: 'Zeynep'
});
it('test manager validity from API ',inject( [AuthService, HttpTestingController],(service : AuthService,http : HttpTestingController)=> {
spyOn(service,'login').and.returnValue(Observable.of({email : 'emnas@gmail.com' , password : 'Zeynep'}));
service.login(value).subscribe(result =>{
expect(result).toBeDefined();
expect(result).toEqual(value),
console.log(result);
});
const req = http.expectOne("https://beta-lp.biodymanager.com/fr/api/login_check");
expect(req.request.method).toEqual('POST');
expect(req.request.body).toEqual(value);
req.flush({user: {email :'emna@gmail.com'}, token: 'xxx'});
this is my authservice
export class AuthService extends RestApiService {
login(body) {
return this.post(SERVICE_LOGIN, body, false, false);
}
the restApiService
export class RestApiService {
// constructor
post(url, data: any, withAuth?: boolean, withLang?: boolean): Observable<any> {
if (typeof withAuth === 'undefined') {
withAuth = true;
}
if (!withAuth) {
this.clearAuthHeader();
}
return new Observable<any>((observer) => {
this.setAuthorizationHeader().then((authorization) => {
this.setLocale().then((language) => {
let apiUrl = this.locale + url;
if (!withLang) {
apiUrl = url;
}
if (this.device.platform === 'Android' || this.device.platform === 'iOS') {
this.globalization.getPreferredLanguage().then(res => {
console.log('current language', res);
const code = res.value.split('-')[0];
if (this.locale === '') {
this.locale = code;
}
if (withLang) {
apiUrl = this.locale + url;
}
console.log('Post request - apiUrl:', apiUrl);
const body = JSON.stringify(data);
let host = '*';
if (!withAuth) {
host = '-';
}
this.http.post(appConfig.getBaseUrl() + apiUrl, body, this.http.getHeaders(host)).then((response) => {
console.log('addNewMeasurement - response.data: ', response.data);
observer.next(JSON.parse(response.data));
observer.complete();
}).catch((error) => {
console.log('addNewMeasurement - error: ', error);
observer.error(error);
observer.complete();
});
}).catch(e => console.log(e));
} else {
if (!withAuth) {
this.clearAuthHeader();
}
if (this.locale === '') {
this.locale = 'en';
}
if (withLang) {
apiUrl = this.locale + url;
}
const body = JSON.stringify(data);
this.httpClient.post(appConfig.getBaseUrl() + apiUrl, body, { headers: new HttpHeaders(this.headers) }).subscribe((result) => {
observer.next(result);
observer.complete();
}, (error) => {
observer.error(error);
observer.complete();
});
}
});
});
});
}
this is appconfig file which contains the url api
testBaseUrl: 'https://beta-lp.biodymanager.com/'
export const SERVICE_LOGIN: string = 'api/login_check';
am i doing somthing wrong? thank you in advance
Try to change
const req = http.expectOne("https://beta-lp.biodymanager.com/fr/api/login_check");
by this
const req = httpMock
.expectOne(req => req.method === 'POST' && req.url === 'https://beta-lp.biodymanager.com/fr/api/login_check');
Or you can check expectOne overloads, there are many ways to call this function
https://angular.io/api/common/http/testing/HttpTestingController#expectOne
Under expectOne() section click on ' 3 overloads '
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.