Using any or anyNamed matchers for nested values with Mockito

Viewed 1314

I'm trying to pass Mockito's argument matchers into nested objects values. I've successfully used something like this so far:

when(object.getData(any, any, userId: anyNamed("userId"))).thenAnswer((_) async => response);

However now I need to use any or anyNamed for nested values. The following code:

when(adapterMock.fetch(
  RequestOptions(
    method: anyNamed("method"),
    path: "orders",
  ),
  any,
  any,
)).thenAnswer((_) async =>response);

Gives an error:

Invalid argument(s): An ArgumentMatcher was declared as named method, but was not passed as an
argument named method.

BAD:  when(obj.fn(anyNamed: "a")))
GOOD: when(obj.fn(a: anyNamed: "a")))

Is there any correct way to provide nested arguments matchers?

1 Answers

It appears that using nested matchers is not possible in mockito. I ended up restructuring my project a little bit so I don't need to use such matchers anymore.

Related