I encountered the same problem, despite the fact that I tested the components that moved the test to another folder, and I managed to check the component.
Here is an example that works, in my component there is a boolean name "isLoggedIn", which I checj if the user is registered
import { async, TestBed } from '@angular/core/testing'; import { BrowserDynamicTestingModule, platformBrowserDynamicTesting } from '@angular/platform-browser-dynamic/testing'; import { MyComponent } from './MyComponent'; import { NO_ERRORS_SCHEMA } from '@angular/core'; import { UserService } from '../../app/shared/services/userService'; describe('ContentComponentTest', () => { // Instance of data-model.spec let dataModel = new DataModels(); let userServiceStub: any = { getUserCookie() { return 'John'; } }; beforeAll(() => { TestBed.initTestEnvironment(BrowserDynamicTestingModule, platformBrowserDynamicTesting()); }); beforeEach(() => { TestBed.configureTestingModule({ declarations: [MyComponent], providers: [ { provide: UserService, useValue: userServiceStub }], schemas: [NO_ERRORS_SCHEMA] }); }); it('Test if user isLoggedIn', async(() => { // Overrides here, if you need them TestBed.overrideComponent(MyComponent, { set: { template: '<div>Overridden template here</div>' // ... } }); TestBed.compileComponents().then(() => { const fixture = TestBed.createComponent(MyComponent); // Access the dependency injected component instance const app = fixture.componentInstance; expect(app.isLoggedIn).toBe(false); // Detect changes as necessary app.ngOnInit(); fixture.detectChanges(); expect(app.isLoggedIn).toBe(true); }); })); });
However, for suppliers injected into a specific component, you will have to mock them. For instance:
providers: [ {provide: UserService, useValue: userServiceStub } ]
source share