【发布时间】:2020-03-13 04:23:28
【问题描述】:
我是打字稿的新手。我的 Nestjs 项目应用程序是这样的。我正在尝试使用存储库模式,所以我将业务逻辑(服务)和持久性逻辑(存储库)分开
用户存储库
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { UserEntity } from './entities/user.entity';
@Injectable()
export class UserRepo {
constructor(@InjectRepository(UserEntity) private readonly repo: Repository<UserEntity>) {}
public find(): Promise<UserEntity[]> {
return this.repo.find();
}
}
用户服务
import { Injectable } from '@nestjs/common';
import { UserRepo } from './user.repository';
@Injectable()
export class UserService {
constructor(private readonly userRepo: UserRepo) {}
public async get() {
return this.userRepo.find();
}
}
用户控制器
import { Controller, Get } from '@nestjs/common';
import { UserService } from './user.service';
@Controller('/users')
export class UserController {
constructor(private readonly userService: UserService) {}
// others method //
@Get()
public async getUsers() {
try {
const payload = this.userService.get();
return this.Ok(payload);
} catch (err) {
return this.InternalServerError(err);
}
}
}
我如何为存储库、服务和控制器创建单元测试而不实际将数据持久化或检索到数据库(使用模拟)?
【问题讨论】:
标签: node.js typescript jestjs nestjs typeorm