#resolve/get injected model

5 messages · Page 1 of 1 (latest)

dense badge
#

Hello, I have created a test module:

  const m = await Test.createTestingModule({
    imports: [
      MongooseModule.forRoot(""),
      MongooseModule.forFeature([{ name: Role.name, schema: RoleSchema }]),
    ],
    controllers: [RolesModule],
    providers: [RoleService],
  }).compile();
  const service = m.get(RoleService);

and here’s the beginning of my RoleService class:

@Injectable()
export class RoleService {
  constructor(
    @InjectModel(Role.name) private roleModel: Model<Role>
  ) {}

How can i get a reference to roleModel using m?
I know I can just write service.roleModel but there is a better way right?

deep storm
#

m.get(getModelToken(Role.name)) should be the right approach here. getModelToken should come from @nestjs/mongoose

barren kayak
#

you could also use the package nestjs-injection-token-resolver

InjectionTokenFor( InjectModel(Role.name) )
// is the same as
getModelToken(Role.name)

if you don't want to memorize the getModelToken function (and others from another lib)

dense badge
dense badge