gpt4 book ai didi

unit-testing - 单元测试没有 ChangeDetectorRef 的提供者

转载 作者:太空狗 更新时间:2023-10-29 18:31:28 27 4
gpt4 key购买 nike

版本:Angular 4。

我正在将一个父组件注入(inject)到子组件中,以通过子组件访问父方法。

在父组件中,我不得不使用 ChangeDetectorRef,因为它有一个会更新运行时的属性。

现在代码工作正常,但子组件规范为注入(inject)父组件的“ChangeDetectorRef”抛出错误。

Error: No provider for ChangeDetectorRef!

父组件

import { Component, AfterViewChecked, ChangeDetectorRef, ChangeDetectionStrategy } from '@angular/core';

@Component({
selector: 'parent-component',
templateUrl: './parent.component.html',
styleUrls: ['./parent.component.scss'],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class ParentComponent implements AfterViewChecked {

stageWidth: number;

constructor(private ref: ChangeDetectorRef) {
this.ref = ref;
}

//call this function as many times as number of child components within parent-component.
parentMethod(childInterface: ChildInterface) {
this.childInterfaces.push(childInterface);
}

ngAfterViewChecked() {
this.elementWidth = this.updateElementWidthRuntime();
this.ref.detectChanges();
}
}

子组件

import { Component, AfterViewInit } from '@angular/core';
import { ParentComponent } from '../carousel.component';

@Component({
selector: 'child-component',
templateUrl: './carousel-slide.component.html',
styleUrls: ['./carousel-slide.component.scss'],
})
export class ChildComponent implements AfterViewInit {

constructor(private parentComponent: ParentComponent) {
}

ngAfterViewInit() {
this.parentComponent.parentMethod(this);
}
}

应用.html

<parent-component>
<child-component>
<div class="box">Content of any height and width</div>
</child-component>
<child-component>
<div class="box">Content of any height and width</div>
</child-component>
<child-component>
<div class="box">Content of any height and width</div>
</child-component>
</parent-component>

子组件的单元测试

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

import { ParentComponent } from '../parent.component';
import { ChildComponent } from './child.component';

describe('ChildComponent', () => {
let component: ChildComponent;
let fixture: ComponentFixture<ChildComponent>;

beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ChildComponent],
providers: [ParentComponent]
})
.compileComponents();
}));

beforeEach(() => {
fixture = TestBed.createComponent(ChildComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});

it('should create \'ChildComponent\'', () => {
expect(component).toBeTruthy();
});
});

当我尝试在子规范的提供程序中添加 ChangeDetectorRef 时,出现以下错误。

Failed: Encountered undefined provider! Usually this means you have a circular dependencies (might be caused by using 'barrel' index.ts files.)

在谷歌搜索和尝试很多事情后我无法找到解决方案。任何帮助表示赞赏。

最佳答案

您正在使用一个组件作为提供者,这可能不是您的本意。更改您的测试模块以声明您的父组件和子组件。

TestBed.configureTestingModule({
declarations: [ChildComponent, ParentComponent],
providers: []
})

关于unit-testing - 单元测试没有 ChangeDetectorRef 的提供者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43106708/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com