gpt4 book ai didi

angular - 如何在按钮标签angular2中测试routerLink

转载 作者:行者123 更新时间:2023-12-03 23:18:28 24 4
gpt4 key购买 nike

按钮看起来像这样

<button [routerLink]="['/account/recovery','identification']" class="btn btn-default">Back</button>

我想检查 url 是否重定向到 /account/recovery/identification单击按钮后

如果是 anchor 标签 solution is provided here .我的问题是按钮标签。

我的测试规范看起来像这样。
beforeEach(async(() => {
let ne = new NavigationEnd(0, "/account/recovery/otp", null); // Create a mock instance for navigation end, utilized within OnInit()
router = {
navigate: jasmine.createSpy('navigate'), // to spy on the url that has been routed
events: new Observable(observer => { // last router event that occurred
observer.next(ne);
}),

};
TestBed
.configureTestingModule({
imports: [CoreModule],
declarations: [OtpComponent],
providers: [
{provide: Router, useValue: router},
{provide: ActivatedRoute, useValue: router},
{provide: Location, useClass: SpyLocation}, FormBuilder
],
schemas: [NO_ERRORS_SCHEMA]
})
.overrideComponent(OtpComponent, {
set: {
providers: [
{provide: MyModel, useClass: MockModel}
],
}
})
.compileComponents();
}));

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

it('Back button click on OTP screen should redirect to identification screen', async(inject([Location], (location: Location) => {

fixture.detectChanges();

let buttonElements = fixture.debugElement.queryAll(By.css('button')); // fetch all the elements with button tag.

buttonElements[0].nativeElement.click();

fixture.detectChanges();
fixture.whenStable().then(
() => {
expect(location.path()).toBe(['/account/recovery', 'identification']); // check if url is routed to identification page after back button is clicked
}
);
})));

但它并没有给我想要的结果,这是我得到的:
Chrome 56.0.2924 (Windows 7 0.0.0) OtpComponent Back button click on OTP screen should redirect to identification screen FAILED
Expected '' to be [ '/account/recovery', 'identification' ].
at webpack:///src/app/account/registration/otp/otp.component.spec.ts:775:40 <- src/test.ts:124883:37 [ProxyZone]
at AsyncTestZoneSpec.onInvoke (webpack:///~/zone.js/dist/async-test.js:49:0 <- src/test.ts:98588:39) [ProxyZone]
at ProxyZoneSpec.onInvoke (webpack:///~/zone.js/dist/proxy.js:76:0 <- src/test.ts:99280:39) [ProxyZone]
at Zone.run (webpack:///~/zone.js/dist/zone.js:113:0 <- src/test.ts:148355:43) [ProxyZone => ProxyZone]
at webpack:///~/zone.js/dist/zone.js:535:0 <- src/test.ts:148777:57 [ProxyZone]
Chrome 56.0.2924 (Windows 7 0.0.0): Executed 1 of 147 (1 FAILED) (skipped 146) ERROR (0.96 secs / 0.594 secs)

buttonElements[0].nativeElement.click();似乎不起作用,因为没有注册点击事件。

最佳答案

answer在您共享的链接中也适用于按钮。这是 plnkr和玩。我希望有一种方法可以静态(无点击)检查按钮上的路由器链接。我想你可以检查它的属性并检查'ng-reflect-router-link',但这感觉很脏。 ¯\_(ツ)_/¯

  • 为测试路线创建一个轻量级组件 (DummyComponent)。 (如果测试多条路由,可以多次使用。)
  • 包括RouterTestingModule.withRoutes([...])使用您在测试模块导入中测试的路径的虚拟组件。
  • 进口 Location并将其注入(inject)测试。 RouterTestingModule使用模拟位置策略,因此您可以检查更改。
    @Component({ template: '' })
    class DummyComponent {}

    beforeEach(() => {
    TestBed.configureTestingModule({
    declarations: [AppComponent, DummyComponent],
    imports: [
    RouterTestingModule.withRoutes([
    { path: 'test', component: DummyComponent }
    ])
    ]
    });
    });

    describe('app', () => {
    it('should have a test button', inject([Location], (location) => {
    const fixture = TestBed.createComponent(AppComponent);
    const elem = fixture.debugElement;

    const button = elem.query(e => e.name === 'button');
    expect(!!button).toBe(true);
    expect(button.nativeElement.textContent.trim()).toBe('CLICK FOR BUBBLES');

    button.nativeElement.click();
    fixture.detectChanges();
    fixture.whenStable().then(() => {
    expect(location.path()).toBe('/test');
    });
    }));
    });
  • 关于angular - 如何在按钮标签angular2中测试routerLink,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43366742/

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