Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

167
Visualizações
Prueba de enlace bidireccional con ngModel en Angular2

He leído la documentación para probar el enlace bidireccional en Angular2. Todos los ejemplos son simples, demasiado simples, en realidad.

Parece que solo prueban el out-binding...

Voy a publicar un código para ilustrar:

 import { Component, Input } from "@angular/core"; import { ComponentFixture, async, TestBed, tick, fakeAsync } from "@angular/core/testing"; import { FormsModule } from "@angular/forms"; import { By } from "@angular/platform-browser"; @Component({ selector: 'test', template: ` <input type="text" [(ngModel)]="myValue" /> <div>{{ myValue }}</div> ` }) class TestComponent { @Input() myValue: string } describe('Example', () => { let component: TestComponent let fixture: ComponentFixture<TestComponent> beforeEach(async(() => { TestBed.configureTestingModule({ declarations: [TestComponent], providers: [], imports: [FormsModule] }) .compileComponents() })) beforeEach(() => { fixture = TestBed.createComponent(TestComponent) component = fixture.componentInstance fixture.detectChanges() }) it('should test two-way binding by setting the component member', fakeAsync(() => { const testValue = 'Component member test' component.myValue = testValue // Should be the correct way to test ngModel tick(); fixture.detectChanges(); // Assertion error: Expected '' to equal 'Component member test' // Why wasn't the value set in the textbox? expect(fixture.debugElement.query(By.css('input')) .nativeElement.value).toEqual(testValue) //Yeah, the bananas are working. We have out-binding expect(fixture.debugElement .query(By.css('div')) .nativeElement .textContent ).toEqual(testValue); })) it('should test two-way binding by setting value directly on the native element. But that just tests the out-binding', fakeAsync(() => { const testValue = 'NativeElement test' let element = fixture.debugElement.query(By.css('input')).nativeElement; element.value = testValue // this tests only the out-binding element.dispatchEvent(new Event('input')); tick(); fixture.detectChanges(); //of course this is Ok, we just set it directly expect(fixture.debugElement.query(By.css('input')) .nativeElement.value).toEqual(testValue) //Yeah, the bananas are working. We have out-binding expect(fixture.debugElement .query(By.css('div')) .nativeElement .textContent ).toEqual(testValue); })) })

Entonces, ¿hay alguna manera de probar el componente configurando myValue? Creo que me estoy perdiendo algo...

about 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Intenta intercambiar dos líneas:

 tick(); fixture.detectChanges();

así debería ser

 fixture.detectChanges(); tick();

Significa que primero debe establecer el valor y luego esperar mientras angular actualiza el valor de control porque sucederá dentro Promise.then

https://github.com/angular/angular/blob/4.1.0-rc.0/packages/forms/src/directives/ng_model.ts#L213-L214

Ejemplo de Plunker

about 4 years ago · Santiago Trujillo Relatório

0

debe agregar el atributo de nombre en su etiqueta de entrada html

 <input type="text" [(ngModel)]="myValue" name="myValue"/>
about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda