我有擴展 ItemContainerComponent 的 CakeItemContainerComponent,我嘗試用 ng-mocks 對其進行測驗。這是我想要做的:
@Component({
selector: 'app-item-container',
template: ''
})
export class ItemContainerComponent implements OnInit {
items$: Observable<Item[]>;
constructor(protected itemService: ItemService) {}
ngOnInit(): void {
this.items$ = this.itemService.getItems();
}
}
@Component({
selector: 'app-cake-item-container',
template: '<div ><span>{{ title }}</span><div *ngFor="let item of items$ | async">{{item.name}}</div></div>'
})
export class CakeItemContainerComponent extends ItemContainerComponent implements OnInit {
title: string;
constructor(itemService: ItemService) {
super(itemSerivce);
}
ngOnInit(): void {
super.ngOnInit();
this.title = 'Some title';
}
}
一切正常,但是當我嘗試用 ng-mocks 測驗它時,我得到了
'NG0304: 'app-cake-item-container' is not a known element:
describe('CakeItemContainerComponent', () => {
beforeEach(() => {
return MockBuilder(CakeItemContainerComponent)
.mock(ItemService)
.build();
});
it('should be created', () => {
const fixture = MockRender(CakeItemContainerComponent);
const component = fixture.point.componentInstance;
fixture.detectChanges();
expect(component).toBeDefined();
});
});
我不認為如此基本和經典的測驗會產生任何問題。
如果我收到無法識別的子組件,我不會感到驚訝,這很好。但是我從未收到過訊息,該測驗組件無法識別...我是 ng-mocks 的新手,所以可能我做錯了什么,但是什么?
感謝您的任何想法:)
uj5u.com熱心網友回復:
有2個錯誤:
beforeEach應該從MockBuilderit應該使用MockRender
一個作業示例:https ://codesandbox.io/s/romantic-aryabhata-shgs7s?file=/src/test.spec.ts
describe('CakeItemContainerComponent', () => {
beforeEach(() => {
return MockBuilder(CakeItemContainerComponent)
.mock(ItemService);
// .build(); // <- remove
});
it('should be created', () => {
const fixture = MockRender(CakeItemContainerComponent); // <- change
const component = fixture.point.componentInstance;
fixture.detectChanges();
expect(component).toBeDefined();
});
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/464889.html
上一篇:R資料框中不同組的卡方檢驗
