Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 如何为异步方法编写单元测试?_Angular_Jasmine_Karma Jasmine - Fatal编程技术网

Angular 如何为异步方法编写单元测试?

Angular 如何为异步方法编写单元测试?,angular,jasmine,karma-jasmine,Angular,Jasmine,Karma Jasmine,我有以下测试代码: 从'@angular/common/http/testing'导入{HttpClientTestingModule,HttpTestingController}; 从“@angular/core/testing”导入{inject,TestBed}; 从“./avior backend.service”导入{AviorBackendService}; 描述('AviorBackendService',()=>{ 在每个(()=>TestBed.configureTesting

我有以下测试代码:

从'@angular/common/http/testing'导入{HttpClientTestingModule,HttpTestingController};
从“@angular/core/testing”导入{inject,TestBed};
从“./avior backend.service”导入{AviorBackendService};
描述('AviorBackendService',()=>{
在每个(()=>TestBed.configureTestingModule之前({
导入:[HttpClientTestingModule],
提供者:[AviorBackendService],
}));
它('应该创建',()=>{
const服务:AviorBackendService=TestBed.get(AviorBackendService);
expect(service.toBeTruthy();
});
//注入'done'方法。这将告诉测试套件正在调用异步方法
//如果在特定的超时(通常为5s)内未调用“完成”,则它会将测试标记为失败
它('期望服务以正确的排序获取数据',(完成)=>{
const服务:AviorBackendService=TestBed.get(AviorBackendService);
//tslint:禁用下一行:首选常量
让httpock:HttpTestingController;
service.getUserCollection().subscribe(数据=>{
期望值(数据长度).toBe(7);
const req=httpMock.expectOne('http://localhost:3000/users');
expect(req.request.method).toEqual('GET');//然后我们设置模拟程序返回的伪数据
请求刷新({firstname:'Chad'});
完成();//将测试标记为完成
},done.fail);//如果出现问题,则将测试标记为失败
});
});
我需要为提供的函数编写一个原型测试(然后为其他类似的函数编写其余的测试)。我仍在学习如何编写测试,据我所知,我需要使用mock,但我不知道如何编写。 我要测试的代码是:

getUserCollection(){
//withCredentials非常重要,因为它传递了进行身份验证所需的JWT cookie
返回this.client.get(SERVICE_URL+'users',{withCredentials:true});
}
我的测试代码给出了一个错误:超时-在jasmine指定的超时内没有调用异步回调。默认的超时时间间隔。

更新

My user-collection.model.ts:

import { User } from './user.model';

export interface UserCollection {

    user: User[];

}

import { Role } from './role';

// was class and not interface!
export interface User {
    _id: number;
    mandator?: number;
    loginId: string;
    lastname: string;
    firstname: string;
    password: string;
    eMail: string;
    group?: string;
    role?: Role;
    active?: boolean;
    token?: string;
}

My user.model.ts:

import { User } from './user.model';

export interface UserCollection {

    user: User[];

}

import { Role } from './role';

// was class and not interface!
export interface User {
    _id: number;
    mandator?: number;
    loginId: string;
    lastname: string;
    firstname: string;
    password: string;
    eMail: string;
    group?: string;
    role?: Role;
    active?: boolean;
    token?: string;
}


你在一个错误的位置(一个永远不会被穿越的地方)进行
刷新

请尝试以下操作:

describe('AviorBackendService', () => {
  let httpTestingController: HttpTestingController;
  let service: AviorBackendService;

  beforeEach(() => {
   TestBed.configureTestingModule({
     imports: [HttpClientTestingModule],
     providers: [AviorBackendService],
   });

   httpTestingController = TestBed.get(HttpTestingController);
   service = TestBed.get(AviorBackendService);
  });

  it('should be created', () => {
    expect(service).toBeTruthy();
  });

  it('expects the service to fetch data with proper sorting', () => {
    const mockReponse = { firstName: 'Chad' };

    service.getUserCollection().subscribe(data => {
      expect(data.firstName).toEqual('Chad');
    });
    const req = httpTestingController.expectOne('IN HERE PUT WHATEVER SERVICE_URL + 'users' EQUALS TO');
    expect(req.request.method).toEqual('POST');
    // send the response to the subscribe.
    req.flush(mockResponse);
  });
});

这是一个关于如何在Angular()中测试HTTP服务的好链接。

您在错误的位置(一个永远不会被遍历的位置)执行
刷新

请尝试以下操作:

describe('AviorBackendService', () => {
  let httpTestingController: HttpTestingController;
  let service: AviorBackendService;

  beforeEach(() => {
   TestBed.configureTestingModule({
     imports: [HttpClientTestingModule],
     providers: [AviorBackendService],
   });

   httpTestingController = TestBed.get(HttpTestingController);
   service = TestBed.get(AviorBackendService);
  });

  it('should be created', () => {
    expect(service).toBeTruthy();
  });

  it('expects the service to fetch data with proper sorting', () => {
    const mockReponse = { firstName: 'Chad' };

    service.getUserCollection().subscribe(data => {
      expect(data.firstName).toEqual('Chad');
    });
    const req = httpTestingController.expectOne('IN HERE PUT WHATEVER SERVICE_URL + 'users' EQUALS TO');
    expect(req.request.method).toEqual('POST');
    // send the response to the subscribe.
    req.flush(mockResponse);
  });
});

关于如何在Angular()中测试HTTP服务的一个很好的链接。

我得到了
错误:对于“匹配URL:localhost:3000/users”标准,应该有一个匹配请求,但没有找到。
以及
服务.getUserCollection().subscribe(数据=>{expect(data.firstname).toEqual('Namehere');})它抛出的部分
属性“firstname”在类型“User[]”上不存在。
…是的,因为它不是
localhost:3000/users
。什么是
服务\u URL
?不要放置
localhost:3000/用户
,而是放置
SERVICE\u URL/用户
,但您必须找出
SERVICE\u URL
的实际值并使用它。另一个问题是,在您的服务中有
,那么它是否返回一个用户数组?如果是,则执行
constmockresponse=[{firstName:'Chad'}].SERVICE\u URL是localhost:3000。是的,就是这样。试着做
constmockresponse=[{firstName:'Chad'}作为用户]。关于
SERVICE\u URL
是否为
localhost
,请查看是否为
https://localhost:3000
。而且,在
it
测试的开始,
console.log(service.service\u URL)
。如果
SERVICE\u URL
不是公共的,则只将其作为
console.log
的一部分公开,以查看其实际值。通过将
作为用户
I获得
将类型“{firstName:string;}”转换为类型“User”可能是错误的,因为两种类型都没有充分重叠。如果这是有意的,请先将表达式转换为“未知”。类型“{firstName:string;}”缺少类型“User”中的以下属性:_id、loginId、lastname、firstName和其他2个属性。
No,它是http://而不是https://。如果我放置
console.log
I get
Property“SERVICE\u URL”在类型“AviorBackendService”上不存在。
和get
Property“firstName”在类型“User[]”上不存在。
我如何使SERVICE\u URL公开?我得到
错误:需要一个匹配的请求来满足条件“匹配URL:localhost:3000/用户”,找不到。
以及
服务.getUserCollection().subscribe(数据=>{expect(data.firstname).toEqual('Namehere');})它抛出的部分
属性“firstname”在类型“User[]”上不存在。
…是的,因为它不是
localhost:3000/users
。什么是
服务\u URL
?不要放置
localhost:3000/用户
,而是放置
SERVICE\u URL/用户
,但您必须找出
SERVICE\u URL
的实际值并使用它。另一个问题是,在您的服务中有
,那么它是否返回一个用户数组?如果是,则执行
constmockresponse=[{firstName:'Chad'}].SERVICE\u URL是localhost:3000。是的,就是这样。试着做
constmockresponse=[{firstName:'Chad'}作为用户]。关于
SERVICE\u URL
是否为
localhost
,请查看是否为
https://localhost:3000
。而且,在
it
测试的开始,
console.log(service.service\u URL)
。如果
SERVICE\u URL
不是公共的,则只将其作为
console.log
的一部分公开,以查看其实际值。通过将
作为用户
I获得
将类型“{firstName:string;}”转换为类型“User”可能是错误的,因为两种类型都没有充分重叠。如果这是有意的,请转换表达式