gpt4 book ai didi

Angular Testing 错误 : Cannot read property 'subscribe' of undefined at new RouterLinkWithHref

转载 作者:太空狗 更新时间:2023-10-29 17:31:00 30 4
gpt4 key购买 nike

我不明白我做错了什么,我没有找到任何 RouterLinkWithHref 链接到测试中的任何错误,而且我没有在任何地方使用 RouterLinkWithHref,据我所知知道了:/

谁能帮我解决这个问题:

fdescribe('AddItemComponent: ', () => {
let cmp: AddItemComponent;
let fixture: ComponentFixture<AddItemComponent>;
let de: DebugElement;
let el: HTMLElement;

const mockRouter = {
navigate: jasmine.createSpy('navigate')
};
const mockCustomCardService = {
getItemDeliveryDate: () => Observable.empty(),
emitItemChanges: () => Observable.empty(),
};
const mockActivatedRoute = { snapshot: {} };
const mockPageDataService = {};
const mockUserService = {};

beforeEach(() => {
TestBed.configureTestingModule({
declarations: [AddItemComponent],
imports: [FormsModule, ReactiveFormsModule, SharedModule, TranslateModule.forRoot(), RouterTestingModule],
providers: [
SpinningService,
FormBuilder,
TranslateService,
{ provide: Router, useValue: mockRouter },
{ provide: CustomItemService, useValue: mockCustomItemService },
{ provide: PageDataService, useValue: mockPageDataService },
{ provide: UserService, useValue: mockUserService },
{ provide: ActivatedRoute, useValue: mockActivatedRoute }
],
schemas: [NO_ERRORS_SCHEMA]
});

fixture = TestBed.createComponent(AddItemComponent);
cmp = fixture.componentInstance;
fixture.detectChanges();
});

it('Simple test', () => {
expect(true).toBeTruthy();
});

});

最佳答案

你应该删除你的路由模拟(mockRoutermockActivatedRoute)因为你已经导入了RouterTestingModule,像这样:

describe('AddItemComponent: ', () => {
let cmp: AddItemComponent;
let fixture: ComponentFixture<AddItemComponent>;
let de: DebugElement;
let el: HTMLElement;

const mockCustomCardService = {
getItemDeliveryDate: () => Observable.empty(),
emitItemChanges: () => Observable.empty(),
};
const mockPageDataService = {};
const mockUserService = {};

beforeEach(() => {
TestBed.configureTestingModule({
declarations: [AddItemComponent],
imports: [FormsModule, ReactiveFormsModule, SharedModule, TranslateModule.forRoot(), RouterTestingModule],
providers: [
SpinningService,
FormBuilder,
TranslateService,
{ provide: CustomItemService, useValue: mockCustomItemService },
{ provide: PageDataService, useValue: mockPageDataService },
{ provide: UserService, useValue: mockUserService }
],
schemas: [NO_ERRORS_SCHEMA]
});

fixture = TestBed.createComponent(AddItemComponent);
cmp = fixture.componentInstance;
fixture.detectChanges();
});

it('Simple test', () => {
expect(true).toBeTruthy();
});

});

关于 Angular Testing 错误 : Cannot read property 'subscribe' of undefined at new RouterLinkWithHref,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45589962/

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