我尝试第一次测试Nest.js应用程序。但是,当我尝试运行它时,几乎默认情况下cli创建的每个测试都失败了……我读到它是数据库连接的问题,所以我尝试使用mongodb-memory-server,但它仍然不会成功。工作。这里有人知道如何解决此问题吗?在创建构建管道时,所有测试都必须成功...
auth.service.ts
import { Injectable } from '@nestjs/common';
import { UsersService } from '../users/users.service';
import { JwtService } from '@nestjs/jwt';
@Injectable()
export class AuthService {
constructor(private readonly usersService: UsersService, private jwtService: JwtService) {}
async validateLoginUser(username: string, pass: string): Promise<any> {
const user = await this.usersService.findByUsernameOrEmail(username);
if (user && await user.comparePassword(pass)) {
return user;
}
return null;
}
async validateFacebookUser(profile): Promise<any> {
return await this.usersService.findByFacebookIdOrEmail(profile.id, profile.emails[0].value);
}
async validateJwtUser(payload): Promise<any> {
return await this.usersService.findByJwtPayload(payload);
}
async login(user: any) {
const payload = { username: user.username, sub: user.id };
return {
access_token: this.jwtService.sign(payload),
};
}
}
auth.service.spec.ts
import { Test, TestingModule } from '@nestjs/testing';
import { AuthService } from './auth.service';
import { MongooseModule } from '@nestjs/mongoose';
import { MongoMemoryServer } from 'mongodb-memory-server';
import { JwtModule } from '@nestjs/jwt';
import { PassportModule } from '@nestjs/passport';
describe('AuthService', () => {
let service: AuthService;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [AuthService],
imports: [
PassportModule.register({defaultStrategy: 'jwt'}),
JwtModule.register({secret: 'secret'}),
MongooseModule.forRootAsync({
useFactory: async () => {
const mongod = new MongoMemoryServer();
const uri = await mongod.getUri();
return {
uri,
};
},
},
)],
}).compile();
service = module.get<AuthService>(AuthService);
});
it('should be defined', () => {
expect(service).toBeDefined();
});
});
错误
Error: Nest can't resolve dependencies of the AuthService (?, JwtService). Please make sure that the argument at index [0] is available in the _RootTestModule context.
at Injector.lookupComponentInExports (/home/olivier/Projects/bp/bp-website-back-nest/node_modules/@nestjs/core/injector/injector.js:183:19)
at Injector.resolveComponentInstance (/home/olivier/Projects/bp/bp-website-back-nest/node_modules/@nestjs/core/injector/injector.js:143:33)
at resolveParam (/home/olivier/Projects/bp/bp-website-back-nest/node_modules/@nestjs/core/injector/injector.js:96:38)
at async Promise.all (index 0)
at Injector.resolveConstructorParams (/home/olivier/Projects/bp/bp-website-back-nest/node_modules/@nestjs/core/injector/injector.js:112:27)
at Injector.loadInstance (/home/olivier/Projects/bp/bp-website-back-nest/node_modules/@nestjs/core/injector/injector.js:78:9)
at Injector.loadProvider (/home/olivier/Projects/bp/bp-website-back-nest/node_modules/@nestjs/core/injector/injector.js:35:9)
at async Promise.all (index 3)
at InstanceLoader.createInstancesOfProviders (/home/olivier/Projects/bp/bp-website-back-nest/node_modules/@nestjs/core/injector/instance-loader.js:41:9)
at /home/olivier/Projects/bp/bp-website-back-nest/node_modules/@nestjs/core/injector/instance-loader.js:27:13
at async Promise.all (index 1)
at InstanceLoader.createInstances (/home/olivier/Projects/bp/bp-website-back-nest/node_modules/@nestjs/core/injector/instance-loader.js:26:9)
at InstanceLoader.createInstancesOfDependencies (/home/olivier/Projects/bp/bp-website-back-nest/node_modules/@nestjs/core/injector/instance-loader.js:16:9)
at TestingModuleBuilder.compile (/home/olivier/Projects/bp/bp-website-back-nest/node_modules/@nestjs/testing/testing-module.builder.js:38:9)
答案 0 :(得分:1)
在Nest.js中,创建提供程序的实例时,所有必需的提供程序都应该可用。在这种情况下,您将创建一个测试模块,但是UserService
:
import
的任何模块中导出(很明显,因为它们是第三方模块,而不是域模块)providers
数组中定义Nest.js告诉您在任何地方都找不到提供程序。
像providers
(和其他必要的东西)一样将其添加到[AuthService, UserService]
中,可使Nest.js能够找到它,加载它并为您提供服务。