预期间谍导航已使用 [ [ 'users' ] ] 调用,但在集成测试 Angular CLI 中从未调用过

2024-04-21

我在一个项目上实现了一个简单的集成测试,但它失败了,因为expect()行是在component.save()方法之前执行的。当我将 Expect() 行放在 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']);

 });
});

error:

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

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

In this HomeComponent是着陆组件并且AppComponent正在拥有router.navigate method.

In my app.component.ts

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

And my app.component.spec.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']);   
    }));
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

预期间谍导航已使用 [ [ 'users' ] ] 调用,但在集成测试 Angular CLI 中从未调用过 的相关文章

  • 角度代理配置不起作用

    我不明白我错在哪里 附 已经尝试通过这个答案修复但仍然不起作用 Angular CLI 代理到后端不起作用 https stackoverflow com questions 39809008 angular cli proxy to ba
  • Angular 5 - 在加载数据之前停止未定义对象的错误

    防止控制台中因仍未定义的对象而出现错误的最佳方法是什么 假设我有这个 name string constructor private data DataService this data name subscribe res gt this
  • 诸如用于测试 HTTP 请求的虚拟 REST 服务器之类的东西? [关闭]

    Closed 此问题正在寻求书籍 工具 软件库等的推荐 不满足堆栈溢出指南 help closed questions 目前不接受答案 我一直在四处寻找 但找不到任何这样的网站 我想知道是否有一些虚拟服务器可以响应测试 GET 请求并返回
  • 资产未考虑基本 href url - 未找到错误

    我刚刚在 Webform 项目中复制了 Angular 构建脚本 并将该项目部署到 IIS 中 注意 我只复制了脚本文件 没有单独部署在 IIS 中 我已将 Angular dist 文件夹复制到 Webform 项目的 Scripts 文
  • Angular Material 7 多重选择 - 设置选定值

    我正在尝试设置多选下拉列表的选定值 如下所示 循环根据条件选择多个复选框并进行设置 document getElementsByTagName mat pseudo checkbox index classList add mat pseu
  • 创建 AoT 兼容的服务工厂

    我正在尝试为缓存服务创建一个服务工厂 主要要求是每次可以使用不同的字符串实例化单个服务 最终的结果会有多个cache每个服务都由唯一定义databaseName 每个缓存可以有一个或多个stores也由唯一定义storeName 其他服务将
  • 对更改运行 PHPUnit 测试

    每当磁盘上的文件发生更改时 我想运行 PHPUnit 测试 或至少其中的一个子集 与您可以使用 进行的操作非常相似 咕噜手表 https github com gruntjs grunt contrib watch blob master
  • Angular2 viewContainerRef.createComponent 工作正常

    我使用 viewContainerRef createComponent 将动态组件加载到根组件 但实际上它附加了错误的位置 my code app compoment ts export class AppComponent privat
  • 使用 VCR 过滤敏感数据

    我正在使用 VCR gem 记录 http 交互并在将来重播它们 我想过滤掉 uri 请求中的实际密码值 以下是 uri 的示例 http services somesite com Services asmx Cabins Usernam
  • 如何在单元测试中模拟 subprocess.call

    我使用的是 python 3 3 我必须测试一个使用的方法call来自 subprocess py I tried subprocess call MagicMock with patch subprocess call as TU cal
  • 当 ViewController 从 UIStoryboard 实例化时,isMemberOfClass 返回 no

    我有一个 OCUnit 测试类 PatientTestViewControllerTests 下面是界面 interface PatientTestViewControllerTests SenTestCase property nonat
  • 无法绑定,因为它不是选择器组件的已知属性

    我想将变量从一个组件传递到另一个组件 并且我正在使用 input 这是我的父组件 Component selector aze templateUrl aze component html styleUrls aze component s
  • 寻找网站测试自动化的方法

    我们开发定制调查网站 我正在寻找一种方法来自动化这些网站的模式测试 调查通常包含许多复杂的规则和分支 这些规则和分支也会根据项目的响应方式而触发 所有调查在发布给客户之前都经过严格测试 此测试需要大量的手动工作 我想了解一些可以通过回答问题
  • Selenium 中的断言和验证

    有人可以解释一下断言和验证之间的区别吗 我知道验证意味着它检查它是否存在 如果不存在 则测试失败并停止在那里 正确吗 那么 即使断言失败了 断言还会继续吗 我已经阅读了文档 但仍然无法理解它 不 你已经搞反了 在 Selenium IDE
  • Angular 2 Material 2 日期选择器日期格式

    我不知道如何更改材料2日期选择器的日期格式 我已阅读文档 但我不明白我实际上需要做什么 datepicker默认提供的输出日期格式为f e 6 9 2017 我想要实现的目标是将格式更改为类似的格式9 Jun 2017或任何其他 文档htt
  • Angular 6 中的 Http 错误处理

    我正在尝试使用 Angular 6 中的以下类来处理 http 错误 我从服务器收到 401 未经授权状态 但我没有看到控制台错误消息 HttpErrorsHandler ts 文件 import ErrorHandler Injectab
  • 单元测试报告的 XML 格式规范(DTD、XSD..)

    许多工具为单元测试报告生成和使用相同的 XML 文件格式 例子 source http junitpdfreport cvs sourceforge net junitpdfreport src resources examples tes
  • Angular with/Angular Material - 对话框主题损坏

    您好 我遇到了 Angular Material 主题在对话框组件中中断的问题 其中文本和其他组件的颜色未按应有的方式工作 在 app component 中 我有一个设置图标按钮来打开对话框 main settings dialog 但当
  • Docker 绑定安装卷不会传播由角度“ngserve”执行监视的更改事件

    请按照下列步骤操作 定义 Dockerfile FROM node alpine RUN yarn global add angular cli RUN yarn global add node sass RUN mkdir volumes
  • Angular Libraries Monorepo:是否可以为每个库使用不同的版本?

    只是一个简单的问题 我似乎找不到答案 我正在尝试构建一个应该能够使用 angular cli v8 的 monorepo 但仍然能够编译和构建为 v2 v3 v4 等制作的库 主要是组件和服务 版本之间发生了很多变化 所以让我们举个例子 V

随机推荐