我找到了如何集成这两个库:
describe('MyComponent', () => {
// SpectatorHost object and factory
let host: SpectatorHost<MyComponent>;
const createHost = createHostFactory({
component: MyComponent,
mocks: [MyService], // Automatically mock service used by the component
});
// MockProxy object from jest-mock-extended
let myServiceMock: MockProxy<MyService>;
// Init and reset service before each test
beforeEach(() => {
myServiceMock = mock<MyService>();
mockReset(MyService);
});
it('...', () => {
// Mock whatever function in the service
myServiceMock.doSomething.mockReturnValue('Mock');
host = createHost('<my-component></my-component>', {
providers: [{ provide: MyService, useValue: myServiceMock }] // Pass mocked service to the component
});
// Rest of the test...
});