-
Notifications
You must be signed in to change notification settings - Fork 93
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
Mocking a single method #182
Comments
I'm not sure I understand. Couldn't you just pass your own method into the |
Right, of course, but then I won't be able to assert if the method was called and if it was, that it was properly called. I'm not sure how I would see the interface for mocking a function though without adding a different A bit like this maybe? // With my example above
export type GameResponseNormalizer = (response: Promise<any>) => Game
// Then to mock this
const normalizer = mock<GameResponseNormalizer >(() => ({id: 2})
normalizer.with(anyNumber()).thenThrow(new Error("Nope"));
// We could then assert it was called properly in a test like so:
verify(normalizer(anything())).once() I have never worked on a mocking library so I have no idea if this proposed API would even make sense. I should note that I managed to do it with what's currently offered, but it's pretty clumsy. const normalizer = mock<{normalizer: GameResponseNormalizer}>()
when(normalizer.normalize(anyString())).thenReturn('Something')
// Then I need to pass the mocked function this way
createGame(whatever, instance(normalizer).normalize)
// Now I can assert the normalizer was properly called
verify(normalizer.normalize(anyString()).atLeast(1) |
Just saw this PR. |
Is ts-mockito able to mock a single function? I've been trying without much success and I'm worried that it's not a supported use case... This lib has been a real blessing to work with so far (it's still pretty new to me) I'd hate to have to switch back to sinon...
Anyway, to a concrete example:
Creating a mock for
RestClient
is pretty basic, but would ts-mockito be able to create a mock forGameResponseNormalizer
?Sometimes it's just not worth building a whole class for very simple behaviors or methods. Especially in a functional language like JS or TS.
The text was updated successfully, but these errors were encountered: