NagRock / ts-mockito

Mocking library for TypeScript
MIT License
974 stars 93 forks source link

Trouble mocking a generic method on a class #189

Open srkinyon-vw opened 4 years ago

srkinyon-vw commented 4 years ago

I have this class I need to mock, the method is giving me problems - how do I set up findThing<T>()?

export interface ISomething { aValue: string; }
export class SomeClass implements ISomething { aValue: string; }

// These classes are unimportant, the method mock is the goal...
export class ParentClass {
    private _obj = new SomeClass();

    findThing<T>(): Observable<T> {
        return of(this._obj as unknown as T);
    }
}

Here's the code I tried:

   const theMock = mock(ParentClass);
   when(theMock.findThing<ISomething>()).thenReturn(of(new SomeClass()));

   const theMockInstance = instance(theMock);
   const result = theMockInstance.findThing();

I get this response:

TypeError: theMockInstance.findThing is not a function

If someone can confirm this is a problem/bug/missing feature that would be helpful.

Nas3nmann commented 3 years ago

I'm also experiencing the same issue, e.g. when trying to mock the Angular http client: this.httpClient.get<SomeType>(url)