简体   繁体   English

NullInjectorError: R3InjectorError(DynamicTestModule)[Router -> Function -> Function]: NullInjectorError: No provider for Function

[英]NullInjectorError: R3InjectorError(DynamicTestModule)[Router -> Function -> Function]: NullInjectorError: No provider for Function

Recently I have started learning angular and in my first project (ToDo API) I have been stuck on a problem occuring when I test the application with: ng test .最近我开始学习 angular 并且在我的第一个项目(ToDo API)中,当我测试应用程序时遇到了一个问题: ng test As you can see the error is in the title.如您所见,错误在标题中。

This is my component, where I get the error:这是我的组件,我得到错误:

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

import { TodosComponent } from './todos.component';

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

  beforeEach(async () => {
    await TestBed.configureTestingModule({
      providers: [ Router ],
      declarations: [ TodosComponent ]
    })
    .compileComponents();
  });

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

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

And this is my app.module.ts:这是我的 app.module.ts:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { HttpClientModule, HTTP_INTERCEPTORS } from '@angular/common/http';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { UserComponent } from './user/user.component';
import { RegistrationComponent } from './user/registration/registration.component';
import { LoginComponent } from './user/login/login.component';
import { TodosComponent } from './user/todos/todos.component';
import { CreateItemComponent } from './user/todos/create-item/create-item.component';
import { UserService } from './shared/user.service';
import { AuthInterceptor } from './auth/auth.interceptor';
import { ShowCompleteDeleteComponent } from './user/todos/show-complete-delete/show-complete-delete.component';

@NgModule({
  declarations: [
    AppComponent,
    UserComponent,
    RegistrationComponent,
    LoginComponent,
    TodosComponent,
    CreateItemComponent,
    ShowCompleteDeleteComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    ReactiveFormsModule,
    FormsModule,
    HttpClientModule
  ],
  providers: [UserService, {
    provide: HTTP_INTERCEPTORS,
    useClass: AuthInterceptor,
    multi: true
  }],
  bootstrap: [AppComponent]
})
export class AppModule { }

I would really appreciate your help!我将衷心感谢您的帮助!

Use RouterTestingModule, and it must be in imports.使用 RouterTestingModule,它必须在导入中。

await TestBed.configureTestingModule({
  declarations: [ TodosComponent ]
  imports: [ RouterTestingModule ],
})
.compileComponents();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 NullInjectorError:R3InjectorError(DynamicTestModule)[FormBuilder -&gt; FormBuilder]:NullInjectorError:没有FormBuilder的提供者 - NullInjectorError: R3InjectorError(DynamicTestModule)[FormBuilder -> FormBuilder]: NullInjectorError: No provider for FormBuilder R3InjectorError(DynamicTestModule)[Store -&gt; Store]: NullInjectorError: No provider for Store - R3InjectorError(DynamicTestModule)[Store -> Store]: NullInjectorError: No provider for Store NullInjectorError: R3InjectorError(DynamicTestModule)[ApiService -&gt; HttpClient -&gt; HttpClient]: NullInjectorError: 没有 HttpClient 的提供者 - NullInjectorError: R3InjectorError(DynamicTestModule)[ApiService -> HttpClient -> HttpClient]: NullInjectorError: No provider for HttpClient Angular 8 使用 Karma 和 Jasmine 进行测试 NullInjectorError: R3InjectorError(DynamicTestModule)[api -&gt; api]: NullInjectorError: No provider for Z8A5DA52ED126447D359E7 - Angular 8 Testing with Karma and Jasmine NullInjectorError: R3InjectorError(DynamicTestModule)[api -> api]: NullInjectorError: No provider for api NullInjectorError: R3InjectorError(AppModule)[Router -&gt; Router -&gt; Router]: NullInjectorError: No provider for Router - NullInjectorError: R3InjectorError(AppModule)[Router -> Router -> Router]: NullInjectorError: No provider for Router 错误 NullInjectorError: R3InjectorError(n)[_0 -&gt; _0 -&gt; _0]: NullInjectorError: No provider for _0! --- 使用 Angular MatDialog 时 - ERROR NullInjectorError: R3InjectorError(n)[_0 -> _0 -> _0]: NullInjectorError: No provider for _0! --- When using Angular MatDialog 错误 NullInjectorError:R3InjectorError(AppModule) - ERROR NullInjectorError: R3InjectorError(AppModule) 为什么我收到这个错误:NullInjectorError: R3InjectorError(DynamicTestModule)[MedicoService? - Why I'm getting this error : NullInjectorError: R3InjectorError(DynamicTestModule)[MedicoService? 为什么我收到 NullInjectorError: R3InjectorError(DynamicTestModule)[MdbModalRef -&gt; MdbModalRef]:? - Why am i getting NullInjectorError: R3InjectorError(DynamicTestModule)[MdbModalRef -> MdbModalRef]:? 错误 NullInjectorError:R3InjectorError(AppModule)[NgbDropdown -> NgbDropdown -> NgbDropdown]:NullInjectorError:NgbDropdown 没有提供者 - ERROR NullInjectorError: R3InjectorError(AppModule)[NgbDropdown -> NgbDropdown -> NgbDropdown]: NullInjectorError: No provider for NgbDropdown
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM