小编N1g*_*4r3的帖子

如何在Angular 2单元测试中触发ngModel模型更新?

我试图测试包含文本输入元素的组件.我想验证当文本输入的值更改时组件的状态按预期更改.当然,文本输入使用ngModel指令(双向绑定).

虽然组件在运行时工作正常,但我在创建有效的传递测试时遇到了麻烦.我发布了我认为应该在下面工作的内容,以及之后的测试结果.

我究竟做错了什么?

测试:

import {Component} from 'angular2/core';
import {describe, it, injectAsync, TestComponentBuilder} from 'angular2/testing';
import {FORM_DIRECTIVES} from 'angular2/common';
import {By} from 'angular2/platform/common_dom';

class TestComponent {
    static get annotations() {
        return [
            new Component({
                template: '<input type="text" [(ngModel)]="name" /><p>Hello {{name}}</p>',
                directives: [FORM_DIRECTIVES]
            })
        ]
    }
}

describe('NgModel', () => {
    it('should update the model', injectAsync([TestComponentBuilder], tcb => {
        return tcb
            .createAsync(TestComponent)
            .then(fixture => {
                fixture.nativeElement.querySelector('input').value = 'John';
                const inputElement = fixture.debugElement.query(By.css('input'));
                inputElement.triggerEventHandler('input', { target: inputElement.nativeElement });
                fixture.detectChanges();
                expect(fixture.componentInstance.name).toEqual('John');
            });
    })); …
Run Code Online (Sandbox Code Playgroud)

unit-testing angular

7
推荐指数
2
解决办法
8555
查看次数

角度主题订阅无效

我有服务 - >

subject = new Subject<any>();

  constructor (){
    this.subject.next('hello');
  }

  getsubject():Observable<any>{
    return this.subject.asObservable();
  }
Run Code Online (Sandbox Code Playgroud)

和组件 - >

name:string;
  subscription : Subscription;
  constructor(private serv:SService) {
    this.subscription=this.serv.getsubject().subscribe(message=>{console.log('hello');});
  }
Run Code Online (Sandbox Code Playgroud)

正如你所看到的,我正在将hello消息从服务传递给订阅者,但是在组件订阅者中没有获得该值,即使它没有记录任何单词.

掠夺者的例子

rxjs angular

7
推荐指数
1
解决办法
7333
查看次数

标签 统计

angular ×2

rxjs ×1

unit-testing ×1