How to mock ClientProxy microservice for nestjs in ava test framework

302 views Asked by At

I am trying to give the mock data in ClientProxy but I am keep on receiving null. It could be because of returning Observable. But I cannot figure it out how to properly mock the result. I am using ava framework to write the tests

test('get user records', async (t) => {
  const mockedUserRepository: Repository<UserRecord> =
    mock(repository);
  const mockedClientProxy = mock(ClientProxy); // mocking nestjs microservice

  const userService = new UserService(
    instance(mockedUserRepository),
    instance(mockedClientProxy),
  );

  when(mockedClientProxy.send(anything, anything)).thenResolve(
    storedUserForTest as any, //this is what I am expecting to be returned a json
  );
  const result =
    await userService.getUser(2);


  t.is(result, storedUserForTest); // result returns null
  verify(mockedClientProxy.send(anything, anything)).once(); // this is also throwing exception
});

I think I am doing the mocking in a right way but something I am missing out.

0

There are 0 answers