我使用這個組件代碼:
HTML:
<app-input [model]="model" field="name"></app-input>
打字稿:
@Component({
selector: 'app-post-create-edit',
templateUrl: './post-create-edit.component.html',
styleUrls: ['./post-create-edit.component.scss'],
changeDetection: ChangeDetectionStrategy.OnPush
})
export class PostCreateEditComponent {
model: new Post();
}
在我的 spec.ts 檔案中:
describe('PostCreateEditComponent', () => {
let fixture: ComponentFixture<PostCreateEditComponent>;
let component: PostCreateEditComponent;
const datas = POSTS_CREATE_EDIT_DATASET;
const model: PostInterface = new Post([datas[0]);
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [...],
providers: [...],
declarations: [PostCreateEditComponent]
})
.compileComponents()
.then(() => {
fixture = TestBed.createComponent(PostCreateEditComponent);
component = fixture.componentInstance;
debugElement = fixture.debugElement;
component.model = model;
fixture.detectChanges();
});
});
describe('general', () => {
it('create-edit component test', () => {
fixture.whenStable().then(() => {
const container = context.debugElement
.queryAll(By.css(`app-input[field="name"]`));
const input = container[0].queryAll(By.css(context.input.type));
expect(input[0].nativeElement.value).toEqual(model.name);
});
});
});
});
它說Error: "name" input element: Expected 'First post title' to equal ''.
我不明白為什么在 call 之后不更改輸入欄位的值changeDetection()。
我該如何解決這個問題?我想念什么?
uj5u.com熱心網友回復:
我認為問題與您的想法相反。Jasmine 斷言首先給出“找到”值,然后給出期望值。它期待''但發現'First post title'。
我認為。茉莉花的斷言反應總是讓我很困惑^^
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/424119.html
標籤:有角度的 单元测试 测试 angular2-changedetection
