> For the complete documentation index, see [llms.txt](https://cordea.gitbook.io/flutter-test-mockito-recipes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://cordea.gitbook.io/flutter-test-mockito-recipes/arg-verification.md).

# 引数の検証

例えば以下のような class があり、`UpdateProfileUseCase` に渡された profile が正しいか test したいとします。

```dart
class ProfileViewModel {
  ProfileViewModel({
    required Profile profile,
    required UpdateProfileUseCase updateProfileUseCase,
  })  : _profile = profile,
        _updateProfileUseCase = updateProfileUseCase;

  Profile _profile;
  final UpdateProfileUseCase _updateProfileUseCase;

  void onNameChanged(String name) {
    _profile = _profile.copyWith(name: name);
  }

  void onSubmitTapped() {
    _updateProfileUseCase.execute(_profile);
  }
}

class Profile {
  const Profile({
    required this.id,
    required this.groupId,
    required this.name,
    required this.thumbnailUrl,
    required this.location,
  });

  ...
}

class UpdateProfileUseCase {
  void execute(Profile profile) {}
}
```

引数の検証としては主に 2 つ方法があります。

## Recipe

### argThat

`argThat` を使用することで `Matcher` を渡すことができます。

```dart
test('test', () {
  viewModel.onNameChanged('name');

  viewModel.onSubmitTapped();

  verify(updateProfileUseCase.execute(argThat(isNotNull)));
});
```

{% hint style="info" %}
任意の条件を指定したい場合は `predicate` が使えます。

```dart
test('test', () {
  viewModel.onNameChanged('name');

  viewModel.onSubmitTapped();

  verify(updateProfileUseCase
      .execute(argThat(predicate<Profile>((v) => v.name == 'name'))));
});
```

{% endhint %}

### captureAny

`captureAny` によって引数を取得することができます。

```dart
test('test', () {
  viewModel.onNameChanged('name');

  viewModel.onSubmitTapped();

  final profile =
      verify(updateProfileUseCase.execute(captureAny)).captured.first;

  expect(profile.name, 'name');
});
```

## Result

```dart
@GenerateMocks([
  UpdateProfileUseCase,
])
void main() {
  late MockUpdateProfileUseCase updateProfileUseCase;
  late ProfileViewModel viewModel;

  setUp(() {
    updateProfileUseCase = MockUpdateProfileUseCase();
    viewModel = ProfileViewModel(
      profile: Profile(
        id: 0,
        groupId: 0,
        name: '',
        thumbnailUrl: '',
        location: '',
      ),
      updateProfileUseCase: updateProfileUseCase,
    );
  });

  test('test', () {
    viewModel.onNameChanged('name');
  
    viewModel.onSubmitTapped();
  
    verify(updateProfileUseCase.execute(argThat(isNotNull)));
  });
  
  test('test', () {
    viewModel.onNameChanged('name');
  
    viewModel.onSubmitTapped();
  
    verify(updateProfileUseCase
        .execute(argThat(predicate<Profile>((v) => v.name == 'name'))));
  });
  
  test('test', () {
    viewModel.onNameChanged('name');
  
    viewModel.onSubmitTapped();
  
    final profile =
        verify(updateProfileUseCase.execute(captureAny)).captured.first;
  
    expect(profile.name, 'name');
  });
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://cordea.gitbook.io/flutter-test-mockito-recipes/arg-verification.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
