首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >应用程序测试返回"NullInjectorError:没有位置提供程序!“

应用程序测试返回"NullInjectorError:没有位置提供程序!“
EN

Stack Overflow用户
提问于 2019-05-27 19:49:54
回答 4查看 13.3K关注 0票数 14

使用Karma在Angular 7中测试应用程序时,我无法删除subj中的错误。

我已经搜索了许多地方(主要是这里),但要么解决方案不起作用,要么与我的情况无关。

App.component.html:

代码语言:javascript
运行
复制
<app-header></app-header>
<router-outlet></router-outlet>

Header.component.ts:

代码语言:javascript
运行
复制
import { Component, OnInit, ViewChild, ElementRef, AfterViewInit, EventEmitter, Output } from '@angular/core';
import { Router } from '@angular/router';
import { Location } from '@angular/common';

@Component({
    selector: 'app-header',
    templateUrl: './header.component.html',
    styleUrls: ['./header.component.less']
})
export class HeaderComponent implements AfterViewInit, OnInit {
    constructor(private location: Location, private router: Router) { 
        setInterval(() => {
            this.now = new Date();
        }, 1000);
    }
...
    onKeyDown(event: KeyboardEvent): void {
        event.preventDefault();
        if (event.which === this.KEY_ESCAPE){
            if (this.router.url !== '/'){
                this.location.back();
            }
        }
    }

App.component.spec.ts:

代码语言:javascript
运行
复制
import { TestBed, async } from '@angular/core/testing';
import { AppComponent } from './app.component';
import { HeaderComponent } from './header/header.component';
import { RouterOutlet } from '@angular/router';

describe('AppComponent', () => {
  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [
        AppComponent,
        HeaderComponent,
        RouterOutlet
      ],
    }).compileComponents();
  }));

  it('should create the app', () => {
    const fixture = TestBed.createComponent(AppComponent);
    const app = fixture.debugElement.componentInstance;
    expect(app).toBeTruthy();
  });
代码语言:javascript
运行
复制
AppComponent should create the app
Error: StaticInjectorError(DynamicTestModule)[HeaderComponent -> Location]: 
  StaticInjectorError(Platform: core)[HeaderComponent -> Location]: 
    NullInjectorError: No provider for Location!
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2019-05-28 15:48:56

修复它:我所需要做的就是导入RouterModule.forRoot([]),这似乎是一个常见的错误,因为它修复了很多StaticInjectorError(DynamicTestModule)错误。

票数 7
EN

Stack Overflow用户

发布于 2020-04-30 17:05:57

在我的例子中,我已经在使用RouterTestingModule,这不应该引发这个错误。我从错误的位置导入了位置,所以出现了错误。“‘Location”应从此处导入:

代码语言:javascript
运行
复制
import {Location} from '@angular/common';

在您的导入中,您应该只像这样导入RouterTestingModule:

代码语言:javascript
运行
复制
TestBed.configureTestingModule({
      imports: [
        RouterTestingModule.withRoutes(
          [
            {path: 'add', component: DummyComponent, pathMatch: 'full'}
          ]
        )
      ],

然后你可以像这样使用Location:

代码语言:javascript
运行
复制
it('Should navigate to / before + button click', () => {
    const location: Location = TestBed.get(Location);
    expect(location.path()).toBe('');
  });

别忘了从@angular/common导入位置

票数 15
EN

Stack Overflow用户

发布于 2020-03-24 20:40:05

在测试期间,NullInjectorError: No provider for Location!的替代方法是导入RouterTestingModule

例如。mycomponent.spec.ts:

代码语言:javascript
运行
复制
    import { RouterTestingModule } from '@angular/router/testing';
    ...
    beforeEach(() => TestBed.configureTestingModule({
            imports: [ RouterTestingModule ]
    }));
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56325514

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档