角组件
public setupObservables() {
this.formFieldChanged$ = this.formField
.valueChanges
.pipe(
debounceTime(100),
distinctUntilChanged((a, b) => a === b),
)
}
茉莉花测试
import { of } from 'rxjs';
import { marbles } from 'rxjs-marbles/jasmine';
...
it('should update value on debounced formField change', marbles(m => {
const values = { a: "1", b: "2", c: "3" };
const fakeInputs = m.cold('a 200ms b 50ms c', values);
const expected = m.cold('100ms a 250ms c', values);
// works on stackblitz but otherwise gives TS2540 compiler error
// cannot assign to a read-only property
component.formField.valueChanges = fakeInputs;
component.setupObservables();
m.expect(component.formFieldChanged$).toBeObservable(expected);
}));
目的是使用大理石测试来测试Observable
具有 Angular 反应形式的上下文中的代码。
- 这种方法有意义吗?
- 如何最好地模拟
valueChanges
一个FormField
对象? - 有没有更好的方法来构建这类测试?