Skip to content

Methods on abstract implementations are lost when downcasting a mocked type #126

Description

@ps2goat

I'm using typemoq version 2.1.0 with typescript 3.4.5

I found an issue when mocking a type that implements an abstract method from a parent class.

The error seems to occur when the following are true:

  1. a child class implements an abstract function from an abstract parent class.
  2. the child class is downcast to the parent in some method.
  3. the mock is set up to be an IMock of the child type.

Expected functionality: the child could mocked and still be cast to the parent type without losing implemented methods. I don't know if this is a language limitation or not.

I don't have time to set up a working repo + test at the moment, but this is my current use case:

// model setup
abstract class Base {
  abstract myMethod(): Promise<void>;
}

class Child extends Base {
  public async myMethod(): Promise<void> {
  }

  public someCustomFunc(): void {

  }
}

class AService {
  public doStuff(model: Base): void {
    model.myMethod();
  }
}

// typemoq test verifies myMethod was called when invoking AService.doStuff.
const service: AService = new AService();

// does not work, model does not have a `myMethod` function on it when in `doStuff`. (error is thrown)
// I would expect this to work
const mockChild1: IMock<Child> = Mock.ofInstance(new Child());
service.doStuff(mockChild1);

I thought this one was working, but it looks like the function on the Child is still lost once I get into the doStuff method:

// works, has the `myMethod` function on it when in `doStuff`
const mockChild2: IMock<Base> = Mock.ofInstance(<Base>(new Child()));
service.doStuff(mockChild2);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions