Tengo el siguiente componente:
@Component({ selector: 'app-comment-table', templateUrl: './comment-table.component.html', styleUrls: ['./comment-table.component.css'] }) export class CommentTableComponent implements OnInit { @Input() line: string; comments: Array<IComments>; constructor(private socketService: SocketService) { this.comments = []; } //some functions }y estoy tratando de probarlo con la siguiente prueba:
class mockSocket { getComments() { return Observable.of() } }; describe('Comment Table Component', () => { let fixture, comp, el; let mockSock = new mockSocket(); beforeEach(() => { TestBed.configureTestingModule({ imports: [Ng2SmartTableModule], declarations: [CommentTableComponent], providers: [ { provide: SocketService, useValue: mockSock }], }); }); it('should call service', inject([CommentTableComponent], (cmp: CommentTableComponent) => { spyOn(mockSock, 'getComments'); cmp.ngOnInit(); expect(mockSock.getComments).toHaveBeenCalled(); })); });Sin embargo, esta prueba está fallando con el mensaje de error:
Error en injectionError (webpack:///~/@angular/core/@angular/core.es5.js:1231:21 <- client/src/test.ts:6040:86) [ProxyZone] en noProviderError (webpack: ///~/@angular/core/@angular/core.es5.js:1269:0 <- client/src/test.ts:6078:12) [ProxyZone]
Creo que el componente solo necesita el SocketService que se le proporciona, que estoy proporcionando, sin embargo, ¿parece que falta un proveedor en ese error? ¿Qué estoy haciendo mal?
Agregue .compileComponents() a la creación de TestBed y envuélvalo en una función async() :
beforeEach( async( () => { TestBed.configureTestingModule( { imports: [ Ng2SmartTableModule ], declarations: [ CommentTableComponent ], providers: [ { provide: SocketService, useValue: mockSock } ], } ).compileComponents(); } ) ); Si eso no es suficiente, envuelva la inject en un async() también:
it('should call service', async( inject( [ CommentTableComponent ], ( cmp: CommentTableComponent ) => { spyOn( mockSock, 'getComments' ); cmp.ngOnInit(); expect( mockSock.getComments ).toHaveBeenCalled(); } ) ) );