我正试图模拟我的服务的findAll函数。为此,我需要模拟myEntityRepository的findAndCount函数或nestjs-typeorm-paginate节点模块的paginate函数。findAll函数从表中获取记录列表,并使用NodeModule nestjs-typeorm-paginate 进行分页。
尝试一:模拟myEntityRepository 但它失败了,并显示错误和回溯信息。
尝试2:模拟分页 然后我尝试模拟paginate方法,但仍然返回错误:
TypeError: 无法重新定义属性:paginate at Function.defineProperty()
我的.service.spec.ts与Pagination模拟更改)
在撰写这个问题之前: 我尝试了以下文章:
尝试一:模拟myEntityRepository 但它失败了,并显示错误和回溯信息。
TypeError: queryBuilder.limit is not a function
at ../node_modules/nestjs-typeorm-paginate/dist/paginate.js:119:28
at ../node_modules/nestjs-typeorm-paginate/dist/paginate.js:8:71
at Object.<anonymous>.__awaiter (../node_modules/nestjs-typeorm-paginate/dist/paginate.js:4:12)
at paginateQueryBuilder (../node_modules/nestjs-typeorm-paginate/dist/paginate.js:115:12)
at Object.<anonymous> (../node_modules/nestjs-typeorm-paginate/dist/paginate.js:22:15)
at ../node_modules/nestjs-typeorm-paginate/dist/paginate.js:8:71
my.service.ts
import { IPaginationOptions, paginate, Pagination } from 'nestjs-typeorm-paginate'
export class MyService {
constructor(@InjectRepository(MyEntity) private myEntityRepository: Repository<MyEntity>) { }
async findAll(options: IPaginationOptions): Promise<Pagination<MyEntity>> {
try {
return await paginate<MyEntity>(this.myEntityRepository, options)
} catch (error) {
throw error
}
}
}
my.service.spec.ts
describe('MyService Basic GET findAll test cases', () => {
let service: MyService
let repositoryMock: MockType<Repository<MyEntity>>
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [MyService,
{
provide: getRepositoryToken(MyEntity), useFactory: repositoryMockFactory
}
],
}).compile()
service = module.get<MyService>(MyService)
repositoryMock = module.get(getRepositoryToken(MyEntity))
const itemList = [{
id: 1,
my_field: 'a1',
}, {
id: 2,
my_field: 'a2',
}, ]
it('should findAll() the MyEntity', async () => {
expect((await service.findAll(options)).items.length).toBe(itemsList.length)
})
})
const repositoryMockFactory: () => MockType<Repository<MyEntity>> = jest.fn(() => ({
find: jest.fn(entity => entity),
findAndCount: jest.fn(entity => entity),
}))
尝试2:模拟分页 然后我尝试模拟paginate方法,但仍然返回错误:
TypeError: 无法重新定义属性:paginate at Function.defineProperty()
我的.service.spec.ts与Pagination模拟更改)
import * as nestjsTypeormPaginate from 'nestjs-typeorm-paginate' // imported at top
....
....
it('should findAll() the MyEntity', async () => {
const queryDto: QueryMyEntityDto = { customerId: 1 }
const options: IPaginationOptions = { page: 1, limit: 10 }
let paginationMock = jest.spyOn(nestjsTypeormPaginate, 'paginate')
paginationMock.mockImplementation((dto, options) => Promise.resolve({
items: itemList.slice(0, 2),
meta: {
itemCount: 2,
totalItems: 2,
totalPages: 1,
currentPage: 1,
}
}))
repositoryMock.find.mockReturnValue(itemList)
expect((await service.findAll(options)).items.length).toBe(itemsList.length)
})
...
在撰写这个问题之前: 我尝试了以下文章: