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

Generic methods cannot be mocked easily #9

Closed
jjherscheid opened this issue Mar 27, 2018 · 8 comments
Closed

Generic methods cannot be mocked easily #9

jjherscheid opened this issue Mar 27, 2018 · 8 comments

Comments

@jjherscheid
Copy link
Owner

jjherscheid commented Mar 27, 2018

When objects have generic methods it is not easy to mock.
For now you must use the following syntax:

mf.setup(f => f.fightersGeneric).Spy.and.returnValue('tada');

or use the spyOf method to use a spy.

But still looking for other solution to be typesafe....

@ike18t
Copy link
Contributor

ike18t commented Mar 27, 2018

Aww man... you lose type safety. I'd rather:
const mock = new Mock({fightersGeneric: function(arg: T) { return {} as T} });

@jjherscheid
Copy link
Owner Author

jjherscheid commented Mar 27, 2018

I was just looking for a solution... I could not found anything working...
Will your solution work?
.... (testing....)
It seems to be working, but still not real ellegant ;-)

@jjherscheid
Copy link
Owner Author

jjherscheid commented Mar 27, 2018

This will also work.. Is the same as @ike18t but with arrow function:
const mock = new Mock({ fightersGeneric: (arg: T) => ({} as T) })

@jjherscheid
Copy link
Owner Author

Problem is with both solutions is that when you fill in the {} with params... Like:

image

@ike18t
Copy link
Contributor

ike18t commented Mar 27, 2018

I can't see the rest of the code. That definitely would cause an error though being that T is not defined to extend anything and you are casting an object with properties declared.

@jjherscheid
Copy link
Owner Author

jjherscheid commented Mar 27, 2018

Basically someone in trying to mock the new HttpClient from Angular 4+ this has a method get< T > which returns Observable<HttpEvent< T >>.

get<T>(url: string, options: { headers?: HttpHeaders | {[header: string]: string | string[]}, observe: 'events', params?: HttpParams|{[param: string]: string | string[]}, reportProgress?: boolean, responseType?: 'json', withCredentials?: boolean, }): Observable<HttpEvent<T>>;

So T has no definition and can't be converted to anything.
Maybe I am missing something.. But I created this Issue to think about a (hopefully) typesafe solution to test methods like this ;-)...

It does not have to be fixed right away. :D

@ike18t
Copy link
Contributor

ike18t commented Mar 27, 2018

Using ts-mocks seems like the wrong tool to use for that job. Angular provides test helpers for mocking out http requests.
https://angular.io/api/http/testing/MockBackend

@jjherscheid
Copy link
Owner Author

Ha, yes I know.. I also told that ;-). But this triggered me to think about generic methods :D

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

2 participants