Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
ps2goat opened this issue May 23, 2019 · 2 comments
Open

Comments

@ps2goat
Copy link

ps2goat commented May 23, 2019

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);
@ps2goat ps2goat closed this as completed May 23, 2019
@ps2goat
Copy link
Author

ps2goat commented May 23, 2019

There's something else going on.

@ps2goat ps2goat reopened this May 23, 2019
@ps2goat
Copy link
Author

ps2goat commented May 23, 2019

Sorry, I was debugging one more potential fix, and I thought the function was missing in my unmocked scenarios, but I found it under the __proto__ property in the VS Code debugger. My unmocked code works fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant