One long-term problem that I experience when writing unit tests in Scala is how to deal with mocking employees who use functional composition.
Typically, in programming, long chains of calls are considered code smells (“train failure code”), but this is apparently specific to object-oriented structures; Functional languages naturally have long chains of calls. For instance:
obj.step1 andThen obj.step2 andThen obj.step3
where stepXare reuse units and compositions. If, however, objappears as a co-author in another object, it becomes very cumbersome for bullying. With Mockito, this will become something like:
when(obj.step1).thenReturn((x) => x)
when(obj.step2).thenReturn((x) => x)
when(obj.step3).thenReturn((x) => x)
so that the call chain is not interrupted. This is also a very weak test, as the subject may or may not have called step3, so I will have to combine the chain of call chains with additional checks.
I was wondering if there is more than an idiomatic way in Scala to solve such objects when they become dependent? The only options I've considered so far are:
- use deep mockery (e.g. using Mockito), which is usually considered a smell
- use a manual layout that records each step of the composition
Both seem to me not perfect.
source
share