I am using ag-grid
and I need to write Jasmine unit test on a piece of code. This code calls our rest server using a class we called RestService
and I want to use spyOn
to mock those calls and responses. In other places in the code I use spyOn(RestService, 'function').and.returnValue()
and it works no problem at all. This code is automatically called by ag-grid
and it is in a datasource object that has a getRows
function in it. In here it calls the api to return the next page of data. The info can be found on https://www.ag-grid.com/javascript-grid-infinite-scrolling/
var dataSource = {
rowCount: null,
getRows: function(params) {
console.log("asking for " + params.startRow + " to " + params.endRow);
// rest service call happens in here
// something like params.context.mycontext.restService.fetchNextRows(data);
}
}
I have a spyOn
with spyOn(restService, 'fetchNextRows').and.returnValue(Observable.of(something))
but it won't work for this. It tries to actually call the rest service and fails because that is not running during the unit test. Does anyone know a workaround?
my spec.ts uses:
TestBed.configureTestingModule(
imports everything i use + has
providers: [RestService]
).compileComponents();
let restService = TestBed.get("RestService");
spyOn(restService, 'fetchNextRows').and.returnValue(Observable.of("mocked json"));
fixture = TestBed.createComponent(ActionComponent);
component = fixture.componentInstance;