期望真等于假

问题描述:

我想测试Anguar教程中确实没有的角度测试.

I want to test something in angular that really is not in teh Anguar tutorial.

我正在尝试测试,当此输入的值无效时,会输出错误消息,因此,如果担心的问题多于20个,则隐藏的属性为false.

I am trying to test that when the value of this input is invalid teh error message is outputted, so the hidden atributte is false in the case of putting a wor with more than 20 haracters.

  <input #cardInput type="text" class="form-control" name="tarjetaSanitaria" id="field_tarjetaSanitaria"
	                [(ngModel)]="paciente.tarjetaSanitaria" maxlength="20"/>
	            <div [hidden]="!(editForm.controls.tarjetaSanitaria?.dirty && editForm.controls.tarjetaSanitaria?.invalid)">
	                <small class="form-text text-danger"  id="ref"
	                   [hidden]="!editForm.controls.tarjetaSanitaria?.errors?.maxlength"  translateValues="{ max: 20 }">
	                   This field cannot be longer than 20 characters.
	                </small>

我的组件有这个:

 paciente: Paciente = {tarjetaSanitaria: 'ddd'} as Paciente;

我的测试:

       fit ('Blank input is not valid', async(() => {
               
                comp.paciente.tarjetaSanitaria = 'ddddddddddddddddddddddddddddddddddddddddddd' ;
                spyOn(comp, 'save');
              var1 = comp.submitButton.nativeElement;
              var1.click();
              fixture.detectChanges();
              expect(comp.save).toHaveBeenCalledTimes(1);
expect(fixture.debugElement.query(By.css('#ref')).nativeElement.hasAttribute('hidden')).toEqual(false);

                })); 

总是无法说出Expected true等于false.如果我移除了fixture.detectChanges,它就消失了.我做错了什么吗?

It always fails saying Expected true to qual false.IF I remove fixture.detectChanges it aways passes. Have I done something wrong?

表达式

 fixture.debugElement.query(By.css('#ref')).nativeElement.hasAttribute('hidden')

读取的内容就像是在测试 #ref 元素是否具有名为 hidden 的属性一样.如果是这样,结果就不太可能取决于属性的值.

reads as if it's testing if the #ref element has an attribute named hidden. If so, the result is unlikely to depend on the value of the attribute.

如果 nativeElement 属性是DOM中始终具有隐藏"属性的HTMLElement,则应该可以使用

If the nativeElement property is an HTMLElement in the DOM which always has a 'hidden' attribute, you should be able to get its value using

fixture.debugElement.query(By.css('#ref')).nativeElement.getAttribute('hidden')

但是,我无法方便地验证属性的数据类型.如果是字符串,则应期望其值为字符串"false".如果它是布尔值,则可能需要期望其值为布尔值 false .

However I have no way of conveniently verifying the data type of the attribute. If it is a string you should expect its value to be the string "false". If it is a boolean you probably need to expect its value to be the boolean value false.

如果您还不知道隐藏"属性的数据类型,或者该属性中存储的是非值如何,请添加一些调试代码以建立数据类型,然后再继续进行操作.祝你好运!

If you don't know the data type of the "hidden" attribute already, or how true and false are stored in the attribute, add some debugging code to establish the data type before proceeding further. Good luck!