jeudi 23 mai 2019

NestJS how to create new mongoose Model in unittest?

I try to unittest my NestJs Controller class. I already mocked my Service using ts-mockito but now I struggle to create the mongoose Objects I want to return and expect to get returned by the controller. How do I manage to create new Model Object to test with?

this is my Service:

@Injectable()
export class ProjectService {
    constructor(
        @InjectModel('Project') private readonly projectModel: Model<Project>,
        private tagService: TagService,
    ) {} ...

This is my Model

    name: {type: String, required: true},
    description: String,
    created: {type: Date, default: Date.now},
});
export const ProjectSchema = schema;

export interface Project extends Document {
    readonly name: string,
    readonly description: string,
    readonly created: Date,
}

And this is my Module:

@Module({
    imports: [
        MongooseModule.forFeature([{ name: 'Project', schema: ProjectSchema }]),
    ],
    controllers: [
        ProjectController
    ],
    providers: [
        ProjectService,
    ],
})
export class ProjectModule {}

This is my Test:

describe('ProjectController', async () => {
    let projectController: ProjectController;
    let projectServiceMock: ProjectService = mock(ProjectService);
    let projectModel: Model<Project>;

    beforeAll(async () => {
        projectModel = mock(Model);

        const module: TestingModule = await Test.createTestingModule({
            controllers: [ProjectController],
            providers: [
                {
                    provide: ProjectService,
                    useValue: instance(projectServiceMock)
                },
                {
                    provide: 'Project',
                    useValue: instance(projectModel)
                }
            ]
        }).compile();

        projectController = module.get<ProjectController>(ProjectController);
    });

Now I am trying to create a new Object of Project and return it from my service and expect it from the controller:

it('should return Project with id from projectService', async () => {
            const project = new projectModel({name: 'ProjectName', description: 'ProjectDescription'});

            let result = Promise.resolve(project);
            when(projectServiceMock.getById('projectId')).thenReturn(result);

            await expect(projectController.getById('projectId')).toEqual(result);
        });

But I get this error:

Nest cannot find given element (it does not exist in current context)

      25 | 
      26 |         projectController = module.get<ProjectController>(ProjectController);
    > 27 |         projectModel = module.get<Model<Project>>('Project');
         |                               ^
      28 |     });
      29 | 
      30 |     describe('getAll', async () => {

As I think I can read from the error message there must be something wrong with 'getting' the model to the test but I really don't know how I can get the Model without initiating a connection or so... What can I do? Do you have some example code that worked for you?

Aucun commentaire:

Enregistrer un commentaire