代码之家  ›  专栏  ›  技术社区  ›  J-man

如何使用Angular 6中的Jasmine将加载的数据测试为反应形式?

  •  0
  • J-man  · 技术社区  · 8 年前

    我有一个Angular 6应用程序,可以将数据加载到 反应形式 使用 ngrx .我试图对其进行单元测试,但似乎无论我做什么更改,我的表单对象的属性值始终为空。

    这是我的代码:

    HTML

    <form [formGroup]="myForm" novalidate>
        <input id="name" formControlName="name" />
        <input id="age" formControlName="age" />
    </form>
    

    组成部分

    export interface MyObject {
        name: string;
        age: string;
    }
    
    export class MyObjectComponent {
        myObject: MyObject;
        myForm: FormGroup;
    
        constructor(private fb: FormBuilder, private store: Store<fromStore.State>) { }
    
        ngOnInit() {
            this.myForm = this.fb.group({
                name: [null, [Validators.required]],
                age: [null, [Validators.required]]
            });
            this.store.select(fromStore.getMyObject).subscribe(x => this.myForm.patchValue(x));
    }
    

    规格文件

    describe('MyObjectComponent', () => {
        let component: MyObjectComponent;
        let fixture: ComponentFixture<MyObjectComponent>;
        let store: Store<fromStore.State>;
        let initialStateL MyObject;
        let el: DebugElement;
    
        beforeEach(async(() => {
            initialState = {
                name: 'My Name',
                age: 55
            };
            TestBed.configureTestingModule({
                imports: [
                    FormsModule,
                    ReactiveFormsModule,
                    HttpClientModule,
                    StoreModule.forRoot({}),
                    StoreModule.forFeature('my-object', reducer)
                ],
                declarations: [MyObjectComponent],
                providers: []
            })
            .compileComponents().then(() => {
                fixture = TestBed.createComponent(MyObjectComponent);
                component = fixture.componentInstance;
                el = fixture.debugElement;
                spyOn(store, 'dispatch').and.callThrough();
                fixture.detectChanges();
            });
        }));
    
        it('should patch values into form', async(() => {
            expect(component.myForm.controls.age.value).toBe(55);
        }
    }
    

    测试总是失败,因为表单中的值为零。有人知道我做错了什么吗?谢谢

    1 回复  |  直到 8 年前
        1
  •  2
  •   Danny908    8 年前

    以下是详细的分配:

    首先为表单创建模拟值,访问表单并分配值,然后需要触发组件上的更改检测,最后测试输入值。

    我还没有测试这段代码,但我认为你要做的事情已经非常接近了。

    describe('MyObjectComponent', () => {
    
    // Rest of code...
    
    it('should patch values into form', async(() => {
        // Mock value
        const mock_age_value = 55;
    
        fixture = TestBed.createComponent(DualityAccordionTabComponent);
        const compiled = fixture.componentInstance;
    
        // Value assignation
        compiled.myForm.patchValue({
           age: mock_age_value
        });
    
        // Trigger change detection
        fixture.detectChanges();
    
        expect(component.myForm.controls.age.value).toBe(55);
    }
    
    
    }
    
    推荐文章