Ensei Tankado
Ensei Tankado

Reputation: 330

How to capture argument in Mockito Dart version

I am trying to catch argument passes to a function and then make some assertions if one of the argument's property is equal to specific value.

Unfortunately an object is passed, therefore I can not just make a quick assertion according to official docs.

cat.eatFood("Fish");
expect(verify(cat.eatFood(captureAny)).captured.single, ["Fish"]);

I want to achieve something as shown if this pseudocode;

cat.eatFood(fridge);
expect(verify(cat.eatFood(captureAny)).captured.single, fridge.milk == "Milk");

or I just wont to store the argument fridge elsewhere.

Upvotes: 5

Views: 2826

Answers (2)

molundb
molundb

Reputation: 639

This worked for me:

Option 1

final verification = verify(mockedSrv.send(captureAny));
expect(verification.captured[0].milk, "Milk");

Option 2

expect(verify(mockedSrv.send(captureAny)).captured.single.milk, "Milk");

Upvotes: 1

yakov
yakov

Reputation: 11

This should do the trick:

verify(
  cat.eatFood(captureThat(equals("fish"))),
).called(1);

Check out more examples at: https://pub.dev/packages/mockito/example

Upvotes: 1

Related Questions