ts-mocks icon indicating copy to clipboard operation
ts-mocks copied to clipboard

Mocking generic functions separately for different types

Open begrob opened this issue 3 years ago • 3 comments

Hi, thanks for sharing your library. A question - can this library be used to mock separate "generic signatures" of a generic function ?

Using your example:

export interface SomeService {
    get<T>(index: number): Observable<T>;
}

can I create separate mocks for

get<Customer>
get<User>

etc at the same time ?

begrob avatar Sep 29 '21 19:09 begrob

Hi, Thank you for using the library ;-) I haven't tried mocking 2 generic methods at the same time. I don't think it wil work at this moment because internally the mock will implement one spy on the get method. To support this feature somehow the mock should have different spies. But I don't think that's an easy solution. There should be a some kind of switch inside the spy method based on the type. I really like the idea to support this, so I will leave this issue open for now. If you have idea's yourself feel free to try things oud and send PR.

jjherscheid avatar Sep 30 '21 05:09 jjherscheid

Hi Johan, thanks for the reply.

I will have a look at the lib code to see if I can implement something (could be above my head though). But since you said it won't be an easy solution, I wonder if it might be more practical to just create simple wrappers, one for each generic signature that needs mocking. Then it would just be those non-generic wrappers to mock (no issues there). Given this solution, do you think the "multi spy generic mock" enhancement would still be needed ?

Thanks Rob

On Wed, Sep 29, 2021, 10:40 PM Johan Herscheid, @.***> wrote:

Hi, Thank you for using the library ;-) I haven't tried mocking 2 generic methods at the same time. I don't think it wil work at this moment because internally the mock will implement one spy on the get method. To support this feature somehow the mock should have different spies. But I don't think that's an easy solution. There should be a some kind of switch inside the spy method based on the type. I really like the idea to support this, so I will leave this issue open for now. If you have idea's yourself feel free to try things oud and send PR.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/jjherscheid/ts-mocks/issues/39#issuecomment-930800499, or unsubscribe https://github.com/notifications/unsubscribe-auth/AJTWSMHPCNRZZPJU5FPFSHTUEPZ3HANCNFSM5FAV66HQ . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

begrob avatar Oct 01 '21 15:10 begrob

Hi Rob, If you create wrappers around the methods with different names the multi spy generic mock is not needed anymore. Basically this library just adds the Spy functionality which works based on methods names. But because I don't like strings with method names I created this library to create typesafe Spy. When I have time myself I am still interested in trying to find a better solution, but I don't know if there is any :S... Good luck, Johan

jjherscheid avatar Oct 04 '21 04:10 jjherscheid