Make the component test to have a body or mock Renderer2
Other approach would be, mocking service:
let renderer: MockRenderer;
class MockRenderer {
addClass(document: string, cssClass: string): boolean {
return true;
}
}
beforeEach(async( () => {
TestBed.configureTestingModule({
...
providers: [{
provide: Renderer2,
useClass: MockRenderer
}]
}).compileComponents().then(() => {
fixture = TestBed.createComponent(YourComponent);
renderer = fixture.debugElement.injector.get(Renderer2);
});
}));
...
it('should call render', () => {
spyOn(renderer, 'addClass');
...
expect(renderer.addClass).toHaveBeenCalledWith(jasmine.any(Object), 'css-class');
});
Instead of mocking the renderer try to hijack it. This should be working with Angular 6+.
In your component.spec.ts:
let renderer2: Renderer2;
...
beforeEach(async( () => {
TestBed.configureTestingModule({
...
providers: [Renderer2]
}).compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(YourComponent);
// grab the renderer
renderer2 = fixture.componentRef.injector.get<Renderer2>(Renderer2 as Type<Renderer2>);
// and spy on it
spyOn(renderer2, 'addClass').and.callThrough();
// or replace
// spyOn(renderer2, 'addClass').and.callFake(..);
// etc
});
it('should call renderer', () => {
expect(renderer2.addClass).toHaveBeenCalledWith(jasmine.any(Object), 'css-class');
});