florinn/typemoq

Can't use mocks in partial mocks

Opened this issue · 0 comments

This seems like a bug:

class User {
    public getName() { return "John"; }
}

class Greeter {
    private user: User;
    constructor(user: User) {
        this.user = user;
    }
    public sayHello() {
        console.log(`Hello, ${this.user.getName()}!`);
    }
}

const mockUser = TypeMoq.Mock.ofType<User>();
const mockGreeter = TypeMoq.Mock.ofInstance(new Greeter(mockUser.object));

mockUser.setup((x) => x.getName()).returns(() => "Laura");
mockGreeter.callBase = true;

console.log(`user.getName(): ${mockUser.object.getName()}`);
mockGreeter.object.sayHello();

Leads to:

     TypeError: this.user.getName is not a function
      at Object.sayHello (test/foo.test.ts:19:41)
      at MethodInvocation.invokeBase (node_modules/typemoq/dist/Proxy/Proxy/Proxy/Invocation.ts:60:50)
      at InvokeBase.handleIntercept (node_modules/typemoq/dist/InterceptorStrategies.ts:74:24)
      at node_modules/typemoq/dist/InterceptorExecute.ts:22:54
      at arraySome (node_modules/typemoq/node_modules/lodash/lodash.js:726:11)
      at Function.some (node_modules/typemoq/node_modules/lodash/lodash.js:9899:14)
      at InterceptorExecute._.some [as intercept] (node_modules/typemoq/dist/InterceptorExecute.ts:21:9)
      at ProxyES5.proxy (node_modules/typemoq/dist/Proxy/Proxy/Proxy/ProxyES5.ts:137:25)
      at Context.describe.only.it (test/foo.test.ts:33:28)