当前位置: 首页 > 知识库问答 >
问题:

预期spy navigate已被[['用户]]调用,但从未在集成测试CLI中调用过

牛景同
2023-03-14

我在一个项目上实现了一个简单的集成测试,但是失败了,因为预期()行在component.save()方法之前执行。当我把预期()行放在setTimeout()上时,它成功了。没有setTimeout()如何成功?

spec.ts

 import { Observable } from 'rxjs/Rx';
 import { async, ComponentFixture, TestBed } from '@angular/core/testing';
 import { By } from '@angular/platform-browser'; 
 import { DebugElement } from '@angular/core';
 import { Router, ActivatedRoute } from '@angular/Router';
 import { UserDetailsComponent } from './user-details.component';
 import { RouterTestingModule } from '@angular/router/testing';

 class RouterStub {
 navigate(params) { };
 }
 class ActivatedRouteStub {
 params: Observable<any> = Observable.empty();
 }
 describe('UserDetailsComponent', () => {
 let component: UserDetailsComponent;
 let fixture: ComponentFixture<UserDetailsComponent>;

 beforeEach(async(() => {
  TestBed.configureTestingModule({
  imports: [RouterTestingModule],
   declarations: [UserDetailsComponent],
   providers: [
     { provide: Router, useClass: RouterStub },
     { provide: ActivatedRoute, useClass: ActivatedRouteStub }
   ],
 })
   .compileComponents();
}));

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

it('should redirect the user to the users page after saving', () => {
let router = TestBed.get(Router);
let spy = spyOn(router, 'navigate');

component.save();

expect(spy).toHaveBeenCalledWith(['users']);

 });
});

错误:

Expected spy navigate to have been called with [ [ 'users' ] ] but it was never called

共有1个答案

刘海
2023-03-14

我遇到了通过创建mockRouter并检查方法nav()是否已被调用而解决的相同问题。

在这里,HomeComponent是着陆组件,AppComponent拥有路由器。导航方法。

在我的应用程序中。组成部分ts

nav() {
    this.router.navigate(['/home']);
}

还有我的应用程序。组成部分规格ts

import { TestBed, async, ComponentFixture, fakeAsync, tick,inject } from '@angular/core/testing';
import { By, BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { HomeComponent } from './home/home.component';
import { RouterModule, Routes } from '@angular/router';
import { Router, RouterOutlet,ActivatedRoute } from "@angular/router";
import { RouterTestingModule } from '@angular/router/testing';
import * as br from '@angular/platform-browser';

describe('Component:AppComponent', () => {
    let component: AppComponent;
    let fixture: ComponentFixture<AppComponent>;
    let debugElement: DebugElement;
    let location, router: Router;
    let mockRouter;

    beforeEach(() => {
        mockRouter = { navigate: jasmine.createSpy('navigate') };
        TestBed.configureTestingModule({
            imports: [RouterTestingModule.withRoutes([
                { path: 'home', component: HomeComponent }
            ])],
            declarations: [AppComponent, HomeComponent],
            providers: [
                { provide: Router, useValue: mockRouter},
            ]
        });
    });

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

    it('should go home', async(() => {
        fixture.detectChanges();
        component.nav();
        expect(mockRouter.navigate).toHaveBeenCalledWith(['/home']);   
    }));
});
 类似资料:
  • 我正在为Angular7模板中的函数编写单元测试用例。它是一个登录组件,登录功能在http请求中具有router.navigate,以便在正确登录时路由到仪表板。但是我得到了错误 - 错误:应使用[['/ProjectData/MasterSequence']]调用spy navigate,但从未调用过。堆叠(http://localhost:9876/absolute/home/hp/Downl

  • 我在用Gomockhttps://godoc.org/github.com/golang/mock和mockgen 此测试的源代码是: 此源文件的测试用例如下: 当我运行测试时,我得到以下错误: sqs\控制器。go:150:意外呼叫*mocks。嘲笑信使。GetMessage([])位于路径_至_mocks_package/mocks/mock_messenger。go:38因为:预期在路径_到

  • 我的从未被调用,即使我在inapp对话框中单击立即购买,logcat也不会打印任何内容。

  • 我是写测试和使用Mockito的新手。我在Stackoverflow上阅读了类似的主题,并做了建议的更改,确保所考虑的类/接口/方法是开放的。 我试图跟踪这个 模仿构造函数注入的依赖项 这是我目前想出来的测试 但我一直得到的回应是 即使我在测试中没有提到这种方法,我也得到了这种反应。这是我的演示者注册方法。我已经改变了类/接口 同样地 这里是接口 感谢您的帮助。

  • 问题:为什么从不调用? 我在xml配置中扩展了,如 MyEntityManagerFactoryBean MyInterceptor: 更新:[解释为什么自定义脏政策看起来不像我的方式] 我希望每次更改实体中的内容时更新时间戳,但除外。同时,应该是持久的,而不是暂时的(意味着导致实体变脏)。 由于我使用Spring事务和Hibernate模板,有一些细微差别: 1) 我无法在每个setter的末尾

  • 我有以下2个组件,它们应该首先从Mongo中删除文档,然后从Elastic中删除。 主要流程: 服务: 不幸的是,deleteDocumentInMongo从未被调用。我可以在日志中看到bean已正确注册。 我是做错了什么,还是你需要更多的调试信息?如果我窃听手柄,则deleteDocumentInES。执行输入,但忽略mongo流。