typemoq icon indicating copy to clipboard operation
typemoq copied to clipboard

More informative message when a verification fails

Open sgronblo opened this issue 7 years ago • 1 comments

For example I wrote a mock like this

// sendRegistrationCompleEmail is a function that takes some options and returns its result as a promise
const emailMock = TypeMoq.Mock.ofInstance(sendRegistrationCompleteEmail)
emailMock.setup(m =>
  m(TypeMoq.It.isAny()))
    .returns(() => Promise.resolve({}))
    .verifiable(TypeMoq.Times.once())
await handleRequest(emailMock.object)
emailMock.verifyAll()

When I remove the call to the function to verify that the test is working as intended I get a failure message like this:

MockException - invocation count verification failed (expected invocation of Function(It.isAny()) exactly 1 times, invoked 0 times
 Configured setups:
 Function(It.isAny()), once

It also includes a callstack so it's not extremely difficult to find the problem. But it would be nice if you could insert some custom message like "the handler should have called send email once" to make it more obvious.

sgronblo avatar Feb 09 '18 09:02 sgronblo

Adding my two cents to this. My mock looks like this:

repository.setup((mockRepo): Promise<BaseEntity> => mockRepo
        .save(It.isValue(createdEntity)))
        .returns((): Promise<BaseEntity> => Promise.resolve(createdEntity))
        .verifiable();

The verification fails and gives me the following message:

MockException: 'Repository.save({"id":6,"updatedAt":"2019-05-19T03:18:45.248Z","createdAt":"2019-05-19T03:18:45.248Z","deletedAt":null})'

      36 | 
      37 |   public async create(entity: DeepPartial<Entity>): Promise<Entity> {
    > 38 |     const savedEntity = await this.repository.save(entity);
         |                                               ^
      39 |     return savedEntity;
      40 |   }
      41 | }

      at MockException.Exception [as constructor] (../node_modules/typemoq/dist/Error/Error/Error/Exception.ts:3:9)
      at new MockException (../node_modules/typemoq/dist/Error/Error/Error/MockException.ts:20:9)
      at ExtractProxyCall.Object.<anonymous>.ExtractProxyCall.handleIntercept (../node_modules/typemoq/dist/InterceptorStrategies.ts:46:23)
      at ../node_modules/typemoq/dist/InterceptorExecute.ts:22:54
      at arraySome (../node_modules/lodash/lodash.js:726:11)
      at Function.some (../node_modules/lodash/lodash.js:9899:14)
      at InterceptorExecute._.some [as intercept] (../node_modules/typemoq/dist/InterceptorExecute.ts:21:9)
      at ProxyES5.proxy [as save] (../node_modules/typemoq/dist/Proxy/Proxy/Proxy/ProxyES5.ts:137:25)
      at Service.<anonymous> (base/base.service.ts:38:47)

The problem as it turns out is that the mock is being passed an extra object property that it wasn't expecting (causing the verification to fail)

Really not that helpful tbh. It would be much more informative to get something along the lines of "Expected , Received " like node's assert already does...

robertmain avatar May 19 '19 03:05 robertmain