Utiliser ComponentFixture, déclencher la détection de changements avec fixture.detectChanges() et créer des espions avec jasmine.createSpyObj.
fixture.componentInstance) et au DOM (fixture.nativeElement ou debugElement).of(data)).describe('ProfilComponent', () => {
let component: ProfilComponent;
let fixture: ComponentFixture<ProfilComponent>;
let authSpy: jasmine.SpyObj<AuthService>;
beforeEach(async () => {
authSpy = jasmine.createSpyObj('AuthService', ['getUserName', 'deconnexion']);
authSpy.getUserName.and.returnValue('Alice');
await TestBed.configureTestingModule({
imports: [ProfilComponent],
providers: [{ provide: AuthService, useValue: authSpy }]
}).compileComponents();
fixture = TestBed.createComponent(ProfilComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('devrait afficher le nom de l\'utilisateur injecté par le mock', () => {
const banner = fixture.nativeElement.querySelector('.user-name');
expect(banner.textContent).toContain('Alice');
expect(authSpy.getUserName).toHaveBeenCalledTimes(1);
});
});