Empecé a buscar aplicaciones de pruebas unitarias angulares 2, pero estoy atascado incluso en los ejemplos más simples. Solo quiero ejecutar una prueba simple para ver si funciona, básicamente lo que quiero es comparar un valor de la página del título con el de la prueba.
Este es el error que estoy recibiendo, pero no veo de dónde viene el error ya que todo parece estar sincronizado conmigo.
Error: Error: No se puede llamar a Promise.then desde dentro de una prueba de sincronización.
Prueba de unidad:
import { ComponentFixture, TestBed } from '@angular/core/testing'; import { By } from '@angular/platform-browser'; import { DebugElement, Input} from '@angular/core'; import { ToDoComponent } from './todo.component'; import { FormsModule } from '@angular/forms'; describe(("test input "),() => { let comp: ToDoComponent; let fixture: ComponentFixture<ToDoComponent>; let de: DebugElement; let el: HTMLElement; beforeEach(() => { TestBed.configureTestingModule({ declarations: [ ToDoComponent ], imports: [ FormsModule ] }) .compileComponents(); }); fixture = TestBed.createComponent(ToDoComponent); comp = fixture.componentInstance; de = fixture.debugElement.query(By.css("h1")); el = de.nativeElement; it('should display a different test title', () => { comp.pageTitle = 'Test Title'; fixture.detectChanges(); expect(el.textContent).toBe('Test Title423'); }); });Mi componente:
import {Component} from "@angular/core"; import {Note} from "app/note"; @Component({ selector : "toDoArea", templateUrl : "todo.component.html" }) export class ToDoComponent{ pageTitle : string = "Test"; noteText : string =""; noteArray : Note[] = []; counter : number = 1; removeCount : number = 1; addNote() : void { if (this.noteText.length > 0){ var a = this.noteText; var n1 : Note = new Note(); n1.noteText = a; n1.noteId = this.counter; this.counter = this.counter + 1; this.noteText = ""; this.noteArray.push(n1); } } removeNote(selectedNote : Note) :void{ this.noteArray.splice(this.noteArray.indexOf(selectedNote),this.removeCount); } }Mueva la inicialización de su variable dentro de beforeEach.
No debe sacar cosas de TestBed ni administrar el dispositivo o componente en el ámbito de describe . Solo debe hacer estas cosas dentro del alcance de una ejecución de prueba: dentro de beforeEach / beforeAll , afterEach / afterAll o dentro de it .
describe(("test input "), () => { let comp: ToDoComponent; let fixture: ComponentFixture<ToDoComponent>; let de: DebugElement; let el: HTMLElement; beforeEach(async(() => { TestBed.configureTestingModule({ declarations: [ToDoComponent], imports: [FormsModule] }) .compileComponents(); })); beforeEach(() => { fixture = TestBed.createComponent(ToDoComponent); comp = fixture.componentInstance; de = fixture.debugElement.query(By.css("h1")); el = de.nativeElement; }) it('should display a different test title', () => { comp.pageTitle = 'Test Title'; fixture.detectChanges(); expect(el.textContent).toBe('Test Title423'); }); });Ver también
Recibí el mismo error por una razón diferente. Puse una TestBed.get(Dependency) dentro de un bloque de describe . La solución fue moverlo al bloque it .
Equivocado:
describe('someFunction', () => { const dependency = TestBed.get(Dependency); // this was causing the error it('should not fail', () => { someFunction(dependency); }); });Fijado:
describe('someFunction', () => { it('should not fail', () => { const dependency = TestBed.get(Dependency); // putting it here fixed the issue someFunction(dependency); }); });