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; }

相关内容

热门资讯

透视有挂!wpk透视辅助靠谱吗... 透视有挂!wpk透视辅助靠谱吗,pokemmo修改器手机版,细节方法(固有是有挂);大神普及一款德州...
透视透视挂!wepoker免费... 透视透视挂!wepoker免费永久脚本,德州机器人代打脚本,AI教程(切实真的是有挂);(需添加指定...
透视游戏!aapoker辅助器... 透视游戏!aapoker辅助器怎么用,wepoker辅助器是真的吗,AI教程(最初有挂);致您一封信...
透视科技!德普之星辅助正版,德... 透视科技!德普之星辅助正版,德普之星辅助器怎么用,攻略教程(一直真的有挂);实战中需综合运用上述技巧...
透视安装!德普之星透视辅助软件... 大家肯定在之前德普之星透视辅助软件或者德普之星透视辅助软件中玩过透视安装!德普之星透视辅助软件,德普...
透视黑科技!aapoker透视... 透视黑科技!aapoker透视软件,wpk透视辅助方法,攻略教程(一贯是有挂)准备好在wpk透视辅助...
透视透视!wepoker透视脚... 透视透视!wepoker透视脚本苹果版,wpk辅助器,AA德州教程(固有真的有挂);致您一封信;亲爱...
透视脚本!hhpoker开辅助... 透视脚本!hhpoker开辅助软件,德普辅助器辅助器怎么用,软件教程(素来真的有挂);科技安装教程;...
透视模拟器!aapoker辅助... 透视模拟器!aapoker辅助工具存在吗,拱趴大菠萝作弊方法,必赢教程(切实真的是有挂)1、每一步都...
透视了解!wpk辅助器,来玩a... 1、透视了解!wpk辅助器,来玩app破解版,2025教程(竟然真的有挂)(UU poker、wpk...