gpt4 book ai didi

angular - RouterTestingModule 未提供位置提供程序

转载 作者:行者123 更新时间:2023-12-01 07:23:29 25 4
gpt4 key购买 nike

我正在对 Component 进行单元测试它使用了另外两个组件。单击按钮时会创建其他组件

.html

    <div id="homepage-top-div" class="homepage-component-css-grid-container">  ...
<button id="get-question-list-button" [routerLink]="questionListRouterLink" class="btn content-div__button--blue css-grid-item-button-div btn-sm">See Questions</button>
</div>
<div id="practice-component-top-div" class="css-grid-container-div common-styles-div--white"> <!-- 4 rows, 1 column-->
...
<button id="new-question-button" [routerLink]="newQuestionRouterLink" class="btn content-div__button--blue css-grid-item-button-div btn-sm">Create a Question</button>
</div>
</div>

.ts
export class HomepageContentComponentComponent implements OnInit {

public questionListRouterLink="/practice-question-list";
public newQuestionRouterLink="/new-practice-question";


constructor() {

}

ngOnInit() {
}

}

我创建了一个规范,但运行它时出现以下错误 - Error: StaticInjectorError[Location]:
NullInjectorError: No provider for Location!

规范是
import {async, ComponentFixture, fakeAsync, TestBed, tick} from '@angular/core/testing';
import { HomepageContentComponentComponent } from './homepage-content-component.component';
import {RouterTestingModule} from "@angular/router/testing";
import {AppRoutingModule} from "../app-routing.module";
import {Router} from "@angular/router";
import {routes} from '../app-routing.module';
import {NewPracticeQuestionComponent} from "../new-practice-question/new-practice-question.component";
import {PraticeQuestionListComponent} from "../pratice-question-list/pratice-question-list.component";
import {NgModule} from "@angular/core";
import {AppModule} from "../app.module";

fdescribe('HomepageContentComponentComponent', () => {
let component: HomepageContentComponentComponent;
let fixture: ComponentFixture<HomepageContentComponentComponent>;

beforeEach(async(() => {
TestBed.configureTestingModule({
imports:[
AppModule,
AppRoutingModule,
RouterTestingModule.withRoutes(routes) //<-- I SUPPOSE THIS STEP SHOULD PROVIDE ROUTERTESTINGMODULE WITH PROVIDERS FOR LOCATION BUT IT SEEMS IT ISN'T
],
declarations: [ HomepageContentComponentComponent,
NewPracticeQuestionComponent,
PraticeQuestionListComponent]
})
.compileComponents();
}));

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

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

it('should navigate to New Questions Component when New Question button is clicked',fakeAsync(()=>{
let router:Router;
let location:Location;
router = TestBed.get(Router);
location = TestBed.get(Location);
console.log('initial router is ',router);
console.log('initial location is ',location);
router.initialNavigation();
router.navigate(['new-practice-question']);
tick();
console.log('new router is ',router);
console.log('new location is ',location);

expect(location.pathname).toBe('/new-practice-question');
}));
});

最佳答案

选择错误位置的原因以及 import {Location} from "@angular/common"; 的原因有帮助的是,在不导入它的情况下,TypeScript 实际上认为您要使用 window.location 的类型,这与您要查找的类型完全不同。

由于类型是从 Angular 的 DI 注册表中检索所需实例的 token ,因此如果使用错误的类型,您将不会得到任何东西。当您注释掉导入并“点击”时,Location在你的 IDE 中输入,你会看到选择的类型实际上是一个 DOM 接口(interface),而不是 Angular 实现。一旦你拥有 "lib": ["dom"],这个 DOM 接口(interface)就隐含在那里。在您的 tsconfig.json 中。

进口 Location从 Angular 显式遮盖Location从 DOM 中选择正确的类型和正确的 Location实例位于 Angular 的 DI 注册表中。

这实际上是很容易犯的错误(我自己犯的),因为即使您忘记导入 Location , DOM Location被选中,您的 IDE 中根本没有任何反馈,您想知道为什么 DI 失败。

关于angular - RouterTestingModule 未提供位置提供程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53813263/

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