Skip to content

Instantly share code, notes, and snippets.

@amatiasq
Created November 2, 2021 12:20
Show Gist options
  • Save amatiasq/a2e0eea6ef2b2abed75f2832f8efc6cf to your computer and use it in GitHub Desktop.
Save amatiasq/a2e0eea6ef2b2abed75f2832f8efc6cf to your computer and use it in GitHub Desktop.
import { mock, instance, when, verify, anything } from 'ts-mockito';
const mockedFoo: Foo = mock(Foo);
// stub method before execution
when(mockedFoo.getBar(3)).thenReturn('three');
// Getting instance from mock
const foo: Foo = instance(mockedFoo);
// Using instance in source code
foo.getBar(3);
foo.getBar(5);
// Explicit, readable verification
verify(mockedFoo.getBar(3)).called();
verify(mockedFoo.getBar(anything())).called();
// parameter validation
verify(mockedFoo.getBar(4)).never();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment