Unit testing NestJS模拟返回守卫的Mixin
我希望能对我遇到的问题有所了解 我有一个可以产生守卫的混血儿。生成的保护使用注入的服务。以下是mixin的代码:Unit testing NestJS模拟返回守卫的Mixin,unit-testing,dependency-injection,nestjs,Unit Testing,Dependency Injection,Nestjs,我希望能对我遇到的问题有所了解 我有一个可以产生守卫的混血儿。生成的保护使用注入的服务。以下是mixin的代码: import { CanActivate, ExecutionContext, Injectable, mixin, } from '@nestjs/common'; import { GqlExecutionContext } from '@nestjs/graphql'; import { AccountService } from 'src/modules/ac
import {
CanActivate,
ExecutionContext,
Injectable,
mixin,
} from '@nestjs/common';
import { GqlExecutionContext } from '@nestjs/graphql';
import { AccountService } from 'src/modules/account/account.service';
export const ForAccountGuard = (
paramName: string,
{ required = false }: { required?: boolean } = {}
) => {
@Injectable()
class _ForAccountGuard implements CanActivate {
constructor(private readonly accountService: AccountService) {}
async canActivate(context: ExecutionContext) {
const ctx = GqlExecutionContext.create(context);
const accountId = ctx.getArgs()[paramName];
const currentUser = ctx.getContext().user;
if (required && !accountId) {
return false;
}
if (accountId) {
const account = await this.accountService.findUserAccount(
accountId,
currentUser.id
);
return !!account;
}
return true;
}
}
return mixin(_ForAccountGuard);
};
在我对使用此mixin作为防护的解析器的测试中,我正在执行以下操作:
@Query(() => [PropertyEntity])
@UseGuards(ForAccountGuard('accountId'))
async allProperties(@Args() { accountId }: AllPropertiesArgs) {
// <implementation removed>
}
看起来注入的AccountService没有被解析
我不太清楚如何告诉Nest的测试模块重写一个作为mixin的保护。我一直在这样尝试,但似乎不起作用:
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
PropertyResolver,
...
],
})
.overrideGuard(ForAccountGuard)
.useValue(createMock<typeof ForAccountGuard>())
.compile();
);
});
beforeach(异步()=>{
常量模块:TestingModule=等待测试。createTestingModule({
供应商:[
房地产经纪人,
...
],
})
.超控守卫(用于AccountGuard)
.useValue(createMock())
.compile();
);
});
那么,我该如何模拟一个混搭的守卫呢?好吧,在做了一点修补之后,我想出了一个解决方案 放弃
overrideGuard
方法,对整个mixin进行一次直接的玩笑
因此,我创建了一个模拟:
import { CanActivate, Injectable, mixin } from '@nestjs/common';
export const mockForAccountGuard = () => {
@Injectable()
class _ForAccountGuardMock implements CanActivate {
canActivate() {
return true;
}
}
return mixin(_ForAccountGuardMock);
};
然后我用jest.mock
来模拟它:
// in my test file
import { mockForAccountGuard } from '../common/guards/__mocks__/for-account.guard';
import { ForAccountGuard } from '../common/guards/for-account.guard';
jest.mock('../common/guards/for-account.guard', () => ({
ForAccountGuard: mockForAccountGuard,
}));
...
describe('PropertyResolver', () => {
...
beforeEach(() => {
...
const module: TestingModule = await Test.createTestingModule({
...
}).compile() // note, not using overrideGuards here
});
})
而这似乎起到了作用 您使用的是绝对导入还是相对导入?如果您使用的是absolute(导入从src/
开始),那么您需要将适当的映射添加到jest.config.js
文件中
// in my test file
import { mockForAccountGuard } from '../common/guards/__mocks__/for-account.guard';
import { ForAccountGuard } from '../common/guards/for-account.guard';
jest.mock('../common/guards/for-account.guard', () => ({
ForAccountGuard: mockForAccountGuard,
}));
...
describe('PropertyResolver', () => {
...
beforeEach(() => {
...
const module: TestingModule = await Test.createTestingModule({
...
}).compile() // note, not using overrideGuards here
});
})