Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 如何在NativeScript中对表单进行单元测试?_Angular_Unit Testing_Nativescript - Fatal编程技术网

Angular 如何在NativeScript中对表单进行单元测试?

Angular 如何在NativeScript中对表单进行单元测试?,angular,unit-testing,nativescript,Angular,Unit Testing,Nativescript,我正在用Angular 8和NativeScript编写一个应用程序。我正在我的应用程序中构建一些表单,我想为这些表单编写单元测试 以下是一些代码片段: register.html <StackLayout class="input-field"> <Label text="Client Code"></Label> <TextField required cla

我正在用Angular 8和NativeScript编写一个应用程序。我正在我的应用程序中构建一些表单,我想为这些表单编写单元测试

以下是一些代码片段:

register.html

    <StackLayout class="input-field">
        <Label text="Client Code"></Label>
        <TextField 
            required 
            class="input" 
            [class.is-invalid]="clientCodeField.invalid && clientCodeField.dirty"
            [(ngModel)]="clientcode" 
            #clientCodeField="ngModel">
        </TextField>
        <StackLayout class="hr-light"></StackLayout>
    </StackLayout>

register.component.spec.ts

import { RegisterClientCodeComponent } from './register-client-code.component';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { NativeScriptFormsModule } from 'nativescript-angular/forms';

describe('RegisterClientCodeComponent', () => {

    let component: RegisterClientCodeComponent;
    let fixture: ComponentFixture<RegisterClientCodeComponent>;

    beforeEach(() => {

        // refine the test module by declaring the test component
        TestBed.configureTestingModule({
            imports: [NativeScriptFormsModule],
            declarations: [RegisterClientCodeComponent]
        });

        fixture = TestBed.createComponent(RegisterClientCodeComponent);
        component = fixture.componentInstance;
        component.ngOnInit();
    });

    it(' RegisterClientCodeComponent should be defined', () => {
        expect(component).toBeDefined();
    });

    it(' RegisterClientCodeComponent should be defined', () => {
        //let email = component.form.controls['email'];
        //expect(email.valid).toBeFalsy();
    });
});
import{registerclientdecomponent}来自“./register client code.component”;
从“@angular/core/testing”导入{ComponentFixture,TestBed};
从“nativescript/forms”导入{nativescript FormsModule};
描述('RegisterClient Decomponent',()=>{
let组件:RegisterClient分解组件;
let夹具:组件夹具;
在每个之前(()=>{
//通过声明测试组件来优化测试模块
TestBed.configureTestingModule({
导入:[NativeScriptFormsModule],
声明:[RegisterClient分解组件]
});
fixture=TestBed.createComponent(RegisterClient分解组件);
组件=fixture.componentInstance;
组件。ngOnInit();
});
它('应定义RegisterClient分解组件',()=>{
expect(component.toBeDefined();
});
它('应定义RegisterClient分解组件',()=>{
//让email=component.form.controls['email'];
//expect(email.valid).toBeFalsy();
});
});

然而,在单元测试中,is表示组件没有“表单”部分。我如何编写单元测试来检查表单是否有效?

如果将表单包装在
表单中,是否有效?我无法做到这一点。除非您知道如何在NativeScript中执行此操作,否则会出现错误。您似乎正在使用模板驱动表单,但试图将其作为反应表单进行测试。@Manoj我对此完全陌生,请您提供建议。如果您不熟悉模板驱动与反应表单,请查阅官方文档。您可以直接找到文本元素并在其上键入,然后触发更改检测以更新ngModel上的值。