Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular Karma抱怨组件中缺少提供者,但提供者并没有直接注入到组件中_Angular_Unit Testing_Karma Jasmine - Fatal编程技术网

Angular Karma抱怨组件中缺少提供者,但提供者并没有直接注入到组件中

Angular Karma抱怨组件中缺少提供者,但提供者并没有直接注入到组件中,angular,unit-testing,karma-jasmine,Angular,Unit Testing,Karma Jasmine,我有一个Angular应用程序,当使用Karma运行单元测试时,我得到一个错误,即组件中缺少存储的提供者。 存储不是直接注入到组件中,而是注入到注入到组件中的服务中 如果我将StoreModule导入到组件中,错误就会消失,但我不必这样做,因为组件不直接需要存储 错误: 错误:StaticInjectorError(DynamicTestModule)[StoreRootModule->Store]: StaticInjectorError(平台:核心)[StoreRootModule->Sto

我有一个Angular应用程序,当使用Karma运行单元测试时,我得到一个错误,即组件中缺少存储的提供者。 存储不是直接注入到组件中,而是注入到注入到组件中的服务中

如果我将StoreModule导入到组件中,错误就会消失,但我不必这样做,因为组件不直接需要存储

错误:

错误:StaticInjectorError(DynamicTestModule)[StoreRootModule->Store]: StaticInjectorError(平台:核心)[StoreRootModule->Store]: NullInjectorError:存储区没有提供程序

组成部分

export class TestComponent {
    constructor(private testService: TestService) {}
}
服务

@Injectable({
    providedIn: 'root'
})
export class TestService {
    constructor(private store: Store<AppState>) {}
}
@可注入({
providedIn:'根'
})
导出类测试服务{
构造函数(私有存储:存储){}
}
组件单元测试

class MockTestService {

}
describe('TestComponent', () => {
    let component: TestComponent;
    let fixture: ComponentFixture<TestComponent>;
    let resetMemorablePhraseService: MockTestService;

    beforeEach(async(() => {
        TestBed.configureTestingModule({
            declarations: [TestComponent],
            imports: [
                // StoreModule.forRoot({}) // adding this line gets rid of the error
            ],
            providers: [{
                provide: TestService, useClass: MockTestService
            }]
        })
            .compileComponents();
    }));

    beforeEach(() => {
        fixture = TestBed.createComponent(TestComponent);
        component = fixture.componentInstance;
        fixture.detectChanges();
        service = TestBed.get(TestService);
    });
}
类MockTestService{
}
描述('TestComponent',()=>{
let组件:TestComponent;
let夹具:组件夹具;
让resetMemorablePhraseService:MockTestService;
beforeach(异步(()=>{
TestBed.configureTestingModule({
声明:[TestComponent],
进口:[
//forRoot({})//添加此行可以消除错误
],
供应商:[{
提供:TestService,useClass:MockTestService
}]
})
.compileComponents();
}));
在每个之前(()=>{
fixture=TestBed.createComponent(TestComponent);
组件=fixture.componentInstance;
fixture.detectChanges();
service=TestBed.get(TestService);
});
}

如果您在任何其他模块内使用任何组件、提供程序,则需要在“导入”下导入该特定模块

否则它将找不到特定的提供程序并抛出错误。上述问题很常见。要解决它,请导入模块。

我找到了原因

如果父组件的子组件依赖于某个服务

还需要将其添加到TestBed.configureTestingModule中

例如:

child.component.ts

// ChildComponent has dependency to AnyService
@Component({
    selector: 'app-child',
    template: 'I have dependency from a service'
})
export class ChildComponent {
    constructor(private anyService: AnyService) { }
}
// ParentComponent has no dependency from AnyService
// but since ChildComponent a.k.a "<app-child></app-child>" is embedded here,
// it will also be the dependency of this component
@Component({
    selector: 'app-parent',
    template: `
<h1>I don't have dependency from any service</h1>
<app-child></app-child>
`
})
export class ParentComponent { }
parent.component.ts

// ChildComponent has dependency to AnyService
@Component({
    selector: 'app-child',
    template: 'I have dependency from a service'
})
export class ChildComponent {
    constructor(private anyService: AnyService) { }
}
// ParentComponent has no dependency from AnyService
// but since ChildComponent a.k.a "<app-child></app-child>" is embedded here,
// it will also be the dependency of this component
@Component({
    selector: 'app-parent',
    template: `
<h1>I don't have dependency from any service</h1>
<app-child></app-child>
`
})
export class ParentComponent { }
//ParentComponent不依赖于任何服务
//但由于此处嵌入了ChildComponent a.k.a“”,
//它也是该组件的依赖项
@组成部分({
选择器:“应用程序父级”,
模板:`
我不依赖任何服务
`
})
导出类ParentComponent{}

我希望这能帮助所有有同样问题的读者

你需要注入模块为什么?组件本身并不依赖于模块。事实上,它依赖于模块,所有角度的东西都是在模块下创建的。你指的是StoreModule吗?是的,无论你的提供商在哪里,你指的是哪个模块存放在不使用仓库的组件上方。