expect(received). toequal (expected) //深度相等错误



我正在用Jest进行单元测试,并在user.controller.spec.ts文件中得到了错误。

老错误:

PASS  src/app.controller.spec.ts (10.482 s)
PASS  src/users/services/users.service.spec.ts (11.629 s)
FAIL  src/users/controllers/users.controller.spec.ts (12.239 s)
● UsersController › should create a user
expect(received).toEqual(expected) // deep equality
- Expected  - 5
+ Received  + 1
- Object {
-   "birthDate": 2000-06-07T00:00:00.000Z,
-   "email": "xs@gsuj.lk",
-   "name": "aqs",
- }
+ Promise {}
45 |   it('should create a user' , () =>{
46 |     const dto ={id:2, name:'aqs',birthDate: new Date('2000-06-07'), email : "xs@gsuj.lk" };
> 47 |     expect(controller.create(dto)).toEqual({
|                                    ^
48 |
49 |       name:dto.name,
50 |       birthDate :dto.birthDate,
at Object.<anonymous> (src/users/controllers/users.controller.spec.ts:47:36)

旧错误已解决,现在出现新的错误:

PASS  src/app.controller.spec.ts (10.573 s)
PASS  src/users/services/users.service.spec.ts (11.867 s)
FAIL  src/users/controllers/users.controller.spec.ts (12.282 s)
● UsersController › should create
expect(received).toEqual(expected) // deep equality
Expected: {"birthDate": 2000-06-07T00:00:00.000Z, "email": "xs@gsuj.lk", "id": 2, "name": "aqs"}
Received: "user created successfully"
62 |     const data = await controller.create(dto);
63 |
> 64 |          expect(data).toEqual({
|                       ^
65 |           id:2, name:'aqs',birthDate: new Date('2000-06-07'), email : "xs@gsuj.lk"
66 |          })
67 |     expect(controller).toBeDefined();
at src/users/controllers/users.controller.spec.ts:64:23
at fulfilled (src/users/controllers/users.controller.spec.ts:5:58)
Test Suites: 2 failed, 2 passed, 4 total
Tests:       1 failed, 4 passed, 5 total

This isOlderuser.controller.spec.ts文件:

import { Test, TestingModule } from '@nestjs/testing';
import { UsersService } from '../services/users.service';
import { UsersController } from './users.controller';
describe('UsersController', () => {
let controller: UsersController; 

const mockUsersService ={
create: jest.fn(dto => {
return {

...dto
};
}),
update:jest.fn((id,dto) =>({
id,
...dto
}))
}
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [UsersController],
providers:[UsersService],
})
.overrideProvider(UsersService)
.useValue(mockUsersService)
.compile();
//get service module from user module
controller = module.get<UsersController>(UsersController);
});
//check if service available
it('should be defined', () => {
expect(controller).toBeDefined();
});
// it('hould create a user', () => {
//   expect(controller.create({name:'aqs'})).toEqual({
//   });
// });
it('should create a user' , () =>{
const dto ={id:2, name:'aqs',birthDate: new Date('2000-06-07'), email : "xs@gsuj.lk" };
expect(controller.create(dto)).toEqual({
id:dto.id,
name:dto.name,
birthDate :dto.birthDate,
email : dto.email
});
expect(mockUsersService.create).toHaveBeenCalledWith(dto);
});
})

This isnewuser.controller.spec.ts文件:

import { Test, TestingModule } from '@nestjs/testing';
import { UsersService } from '../services/users.service';
import { UsersController } from './users.controller';
describe('UsersController', () => {
let controller: UsersController; 

const mockUsersService ={
create: jest.fn(dto => {
return {

...dto
};
}),
update:jest.fn((id,dto) =>({
id,
...dto
}))
}
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [UsersController],
providers:[UsersService],
})
.overrideProvider(UsersService)
.useValue(mockUsersService)
.compile();
//get service module from user module
controller = module.get<UsersController>(UsersController);
});
//check if service available
it('should be defined', () => {
expect(controller).toBeDefined();
});
// it('hould create a user', () => {
//   expect(controller.create({name:'aqs'})).toEqual({
//   });
// });
// it('should create a user' , () =>{
//   const dto ={id:2, name:'aqs',birthDate: new Date('2000-06-07'), email : "xs@gsuj.lk" };
//   expect(controller.create(dto)).toEqual({
//     id:dto.id,
//     name:dto.name,
//     birthDate :dto.birthDate,
//     email : dto.email
//   });

//   expect(mockUsersService.create).toHaveBeenCalledWith(dto);
// });
it('should create', async () => {
const dto ={id:2, name:'aqs',birthDate: new Date('2000-06-07'), email : "xs@gsuj.lk" };
const data = await controller.create(dto);

expect(data).toEqual({ 
id:2, name:'aqs',birthDate: new Date('2000-06-07'), email : "xs@gsuj.lk" 
})
expect(controller).toBeDefined();
});
})

user.service.spec.tsfile:

import { Test, TestingModule } from '@nestjs/testing';
import { getRepositoryToken } from '@nestjs/typeorm';
import { getRepository } from 'typeorm';
//import { UsersController } from '../controllers/users.controller';
import { UsersService } from './users.service';
import {User} from '../entities/user.entity'
describe('UsersService', () => {
let service: UsersService;
//  let providers:UsersController;
const mockUsersRepository ={
create:jest.fn().mockImplementation(dto =>dto),
save:jest.fn().mockImplementation(user => Promise.resolve({id:2, ...user}))
}
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [UsersService, {
provide:getRepositoryToken(User),
useValue:mockUsersRepository,
},
],
}).compile();
service = module.get<UsersService>(UsersService);
});
it('should be defined', () => {
expect(service).toBeDefined();
});
it('should be create a new user record and return that', async () => {
expect( await service.create({id:2, name:'aqs',birthDate: new Date('2000-06-07'), email : "xs@gsuj.lk" })).toEqual({
id:2, 
name:'aqs',
birthDate: new Date('2000-06-07'), 
email : "xs@gsuj.lk" ,
});
});
});

您的controller.create()返回的承诺不是预期的您应该像这样等待控制器数据:

const data = await controller.create(whatever);
expect(data).toEqual(expectedData)

相关内容

  • 没有找到相关文章

最新更新