Spyon is not working when constructor call a function inside service. Always getting the mockservice response in testcase.if i have set mock service return data as null, i have get that , after spyon i need to throw error,when i did that, it is getting a value from below productMockService
Here I need to test the different Scenorios for below service calls with jasmine.It already written functionality and functionlity working as expected.
But Failure Scenarios are unable to test ,which is not getting into error method of observable.In the Real service, constructor has call the method and get accessor also call the same method for getting the updated values.
In the test case i have given like below,
test case: i am using useValue to mock the service.
it('should call the getproducts and get into error',()=>{
const app = fixture.componentInstance;
spyOn(productMockService, 'modeSetupStaticData').and.returnValue(
throwError(new Error( "Internal server Error" ))
);
(component as any).fetchProduct();
fixture.detectChanges();
expect(component.productType.length).toBe(undefined);
});Spyon is not working when constructor call a function inside service. Always getting the mockservice response in testcase.if i have set mock service return data as null, i have get that , after spyon i need to throw error,when i did that, it is getting a value from below productMockService
Here I need to test the different Scenorios for below service calls with jasmine.It already written functionality and functionlity working as expected.
But Failure Scenarios are unable to test ,which is not getting into error method of observable.In the Real service, constructor has call the method and get accessor also call the same method for getting the updated values.
In the test case i have given like below,
test case: i am using useValue to mock the service.
it('should call the getproducts and get into error',()=>{
const app = fixture.componentInstance;
spyOn(productMockService, 'modeSetupStaticData').and.returnValue(
throwError(new Error( "Internal server Error" ))
);
(component as any).fetchProduct();
fixture.detectChanges();
expect(component.productType.length).toBe(undefined);
});
In the above test case , i have get the productType length as 2, because which is already set in ProductMockService,need returnvalue as throw someError or some value has changed ,which is always getting a data from mock service.its not considering spyon return value.
In Component. I have call this fetchProduct and subscribe and get the data from service.
fetchProduct =() =>{
this.productMockService.modeSetupStaticData.subscribe(respose=>successProduct(data),
err=> failureProduct(err)
});Spyon is not working when constructor call a function inside service. Always getting the mockservice response in testcase.if i have set mock service return data as null, i have get that , after spyon i need to throw error,when i did that, it is getting a value from below productMockService
Here I need to test the different Scenorios for below service calls with jasmine.It already written functionality and functionlity working as expected.
But Failure Scenarios are unable to test ,which is not getting into error method of observable.In the Real service, constructor has call the method and get accessor also call the same method for getting the updated values.
In the test case i have given like below,
test case: i am using useValue to mock the service.
it('should call the getproducts and get into error',()=>{
const app = fixture.componentInstance;
spyOn(productMockService, 'modeSetupStaticData').and.returnValue(
throwError(new Error( "Internal server Error" ))
);
(component as any).fetchProduct();
fixture.detectChanges();
expect(component.productType.length).toBe(undefined);
});
In the above test case , i have get the productType length as 2, because which is already set in ProductMockService,need returnvalue as throw someError or some value has changed ,which is always getting a data from mock service.its not considering spyon return value.
In Component. I have call this fetchProduct and subscribe and get the data from service.
fetchProduct =() =>{
this.productMockService.modeSetupStaticData.subscribe(respose=>successProduct(data),
err=> failureProduct(err)
});
mockservice: In the mock service , i have get the data form mockproduct resposne.
export class productMockService{
modeSetupStaticData:Observable<any> =observableOf(
mockproductResponse.getProduct()
);
}static getProduct(){
return {
productType:[{productId:1,productType:'Mobiles',price:1000},{productId:1,productType:'laptops',price:1000}],
productLocation:[{productId:1,locationID:10001,location:chennai}]
}Spyon is not working when constructor call a function inside service. Always getting the mockservice response in testcase.if i have set mock service return data as null, i have get that , after spyon i need to throw error,when i did that, it is getting a value from below productMockService
Here I need to test the different Scenorios for below service calls with jasmine.It already written functionality and functionlity working as expected.
But Failure Scenarios are unable to test ,which is not getting into error method of observable.In the Real service, constructor has call the method and get accessor also call the same method for getting the updated values.
In the test case i have given like below,
test case: i am using useValue to mock the service.
it('should call the getproducts and get into error',()=>{
const app = fixture.componentInstance;
spyOn(productMockService, 'modeSetupStaticData').and.returnValue(
throwError(new Error( "Internal server Error" ))
);
(component as any).fetchProduct();
fixture.detectChanges();
expect(component.productType.length).toBe(undefined);
});
In the above test case , i have get the productType length as 2, because which is already set in ProductMockService,need returnvalue as throw someError or some value has changed ,which is always getting a data from mock service.its not considering spyon return value.
In Component. I have call this fetchProduct and subscribe and get the data from service.
fetchProduct =() =>{
this.productMockService.modeSetupStaticData.subscribe(respose=>successProduct(data),
err=> failureProduct(err)
});
mockservice: In the mock service , i have get the data form mockproduct resposne.
`
export class productMockService{
modeSetupStaticData:Observable<any> =observableOf(
mockproductResponse.getProduct()
);
}
mockproductResponse.ts
static getProduct(){
return {
productType:[{productId:1,productType:'Mobiles',price:1000},{productId:1,productType:'laptops',price:1000}],
productLocation:[{productId:1,locationID:10001,location:chennai}]
}
ProductService:
@Injectable
export class ProductService{
private check = false;
get modeSetupStaticData():observable<any>{
this.fetchAllProducts();
}
constructor(){
this.fetchAllProducts();
}
fetchAllProducts():any()=>{
this.getproductService.fetchProductList.Subscribe(respose=>ProductSucess(data),
err=> ProductFailure(err)
});
private ProductFailure(err) :void(){
alert("Service has failed");
}
}There has some typo mistakes for syntax closing while typing , this is demo ,The real code is copyrighted ,so not able to post , therefor i have created a scenario like that.
problem:always getting the mock service response in particular test case, i am unable to override that.if any one help means appreciated.
In mock service resposne i given null means it is null in the test case,if provide response means getting respone,unable to change the response by using spyon return value.