-
Notifications
You must be signed in to change notification settings - Fork 93
ts mockito 1.x to 2.x migration guide
The most important thing, why 2.x version breaks current API is to be more compatible with Java Mockito version of recording multiple behaviors and new capturing API. Issues:
- (https://github.com/NagRock/ts-mockito/issues/12)
- (https://github.com/NagRock/ts-mockito/issues/16)
- (https://github.com/NagRock/ts-mockito/issues/19)
- (https://github.com/NagRock/ts-mockito/issues/21)
In 1.x version to return multiple values one by one we had to do:
when(mockedFoo.getBar(anyNumber())).thenReturn('one');
when(mockedFoo.getBar(anyNumber()).thenReturn('two');
when(mockedFoo.getBar(anyNumber())).thenReturn('three');
In 2.x version we can do it like in Java:
when(mockedFoo.getBar(anyNumber())).thenReturn('one').thenReturn('two').thenReturn('three');
// Or even easier with short notation:
when(mockedFoo.getBar(anyNumber())).thenReturn('one', 'two', 'three');
Because in 1.x version recording multiple has been separated to multiple when
calls it was setting return values order. For example:
when(mockedFoo.getBar(anyNumber())).thenReturn('one');
when(mockedFoo.getBar(anyNumber()).thenReturn('two');
when(mockedFoo.getBar(anyNumber())).thenReturn('three');
const foo = instance(mockedFoo);
foo.getBar(1); // one
foo.getBar(1); // two
foo.getBar(1); // three
foo.getBar(1); // null - !!! no more defined values
In 2.x version last stubbing is more important and will be repeated infinitely:
when(mockedFoo.getBar(anyNumber())).thenReturn('one');
when(mockedFoo.getBar(anyNumber()).thenReturn('two');
when(mockedFoo.getBar(anyNumber())).thenReturn('three');
const foo = instance(mockedFoo);
foo.getBar(1); // three - last stubbing overrides previous ones!
foo.getBar(1); // three
foo.getBar(1); // three
But of course you can define multiple return values:
when(mockedFoo.getBar(anyNumber())).thenReturn('one', 'two', 'three');
const foo = instance(mockedFoo);
foo.getBar(1); // one
foo.getBar(1); // two
foo.getBar(1); // three
In 1.x version we had to create Captor
for all arguments, and pass it to thenCapture
method
const mockedFoo:Foo = mock(Foo);
const firstArgCaptor: Captor<number> = new Captor<number>();
const secondArgCaptor: Captor<number> = new Captor<number>();
when(mockedFoo.sumTwoNumbers(anything(), anything())).thenCapture(firstArgCaptor, secondArgCaptor);
In 2.x version we can get arguments much easier:
const mockedFoo:Foo = mock(Foo);
const [firstArg, secondArg] = capture(mockedFoo.sumTwoNumbers).last();
// you can use last(), first(), second(), byCallIndex(3) etc.
// firstArg and secondArg values are strongly typed!!!
As you can see, the only thing you got to change is to replace all multiline calls of stubbing into one, using fluent API. And if you want to get null after your values you got to define it as last one behavior. And replace captors API with new easier one.
Thanks for all issue reporters! And special thanks for Michał Stocki (https://github.com/michalstocki) for long conversations about ts-mockito 2.x API version!