Angular单元测试中,changeDetection()方法执行后,输入值未发生变化。
创始人
2024-10-23 22:30:39
0

1.在测试文件中引入ChangeDetectorRef:

import { Component, ChangeDetectorRef } from '@angular/core'; import { ComponentFixture, TestBed } from '@angular/core/testing';

2.对于每个测试用例,在BeforeEach块中创建一个新的实例,并使用changeDectectorRef的detectChanges方法手动调用变更检测:

describe('MyComponent', () => { let component: MyComponent; let fixture: ComponentFixture; let changeDetectorRef: ChangeDetectorRef;

beforeEach(async () => { await TestBed.configureTestingModule({ declarations: [ MyComponent ] }) .compileComponents(); });

beforeEach(() => { fixture = TestBed.createComponent(MyComponent); component = fixture.componentInstance; changeDetectorRef = fixture.debugElement.injector.get(ChangeDetectorRef); fixture.detectChanges(); });

it('should update input value', () => { component.myInputValue = 'new value'; changeDetectorRef.detectChanges(); expect(component.myInputValue).toEqual('new value'); }); });

3.确保对应的组件输入属性有输入绑定:

@Component({ template: }) export class MyComponent { @Input() myInputValue: string; }

相关内容

热门资讯

透视免费!wepoker公共底... 透视免费!wepoker公共底牌(透视)都是真的是有挂(详细辅助揭秘攻略)1、不需要AI权限,帮助你...
透视ai!德普辅助器辅助器怎么... 透视ai!德普辅助器辅助器怎么用(透视)一贯真的是有挂(详细辅助细节揭秘)1)辅助挂:进一步探索辅助...
玩家必备科技!德州hhpoke... 玩家必备科技!德州hhpoker是真的吗,hhpoker万能辅助器,靠谱教程(有挂教程)是一款可以让...
透视了解!pokemmo手机版... 透视了解!pokemmo手机版脚本免费(透视)真是真的有挂(详细辅助解说技巧);1、用户打开应用后不...
一分钟揭秘!pokemmo手机... 1、一分钟揭秘!pokemmo手机版脚本免费,wepoker私人局辅助,wpk教程(有挂透视);详细...
透视辅助!wepoker辅助是... 透视辅助!wepoker辅助是真的假的(透视)真是是真的有挂(详细辅助黑科技教程)1、透视辅助!we...
透视数据!cloudpoker... 透视数据!cloudpoker怎么开挂(透视)切实是真的有挂(详细辅助技巧教程)1、操作简单,无需注...
盘点一款!hhpoker有没有... 1、盘点一款!hhpoker有没有作弊挂,pokerworld破解版下载,力荐教程(有挂技巧)。2、...
透视玄学!pokemmo辅助器... 透视玄学!pokemmo辅助器(透视)真是是真的有挂(详细辅助力荐教程)运辅助工具,进入游戏界面。进...
一分钟带你了解!德普之星辅助工... 一分钟带你了解!德普之星辅助工具如何打开,pokemmo手机版修改器,黑科技教程(有挂透视)准备好在...