---
title: "Discussion on \"Mock IteratorAggregate with Mockery to Fix foreach in Tests\" | Hashnode"
description: "Discussion on \"Mock IteratorAggregate with Mockery to Fix foreach in Tests\". Mocking IteratorAggregate breaks foreach in PHPUnit. Return an ArrayObject from the mocked getIterator method to make foreach iterate over test data correctly."
url: "https://hashnode.com/posts/how-to-use-mockery-to-mock-iteratoraggregate/69a63dccb5b0a9cd95d01fd0"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [Mock IteratorAggregate with Mockery to Fix foreach in Tests](https://recca0120.hashnode.dev/how-to-use-mockery-to-mock-iteratoraggregate?utm_source=hashnode&utm_medium=feed)

Mocking IteratorAggregate breaks foreach in PHPUnit. Return an ArrayObject from the mocked getIterator method to make foreach iterate over test data correctly.

Mar 3 · 1 min read · recca0120.hashnode.dev  
Read the article

-   [#mockery](https://hashnode.com/tag/mockery)
-   [#php](https://hashnode.com/tag/php)
-   [#phpunit](https://hashnode.com/tag/phpunit)
-   [#testing](https://hashnode.com/tag/testing)

## Written by

[recca0120](https://hashnode.com/@recca0120)

@recca0120 · Taipei, Taiwan

  
[View profile](https://hashnode.com/@recca0120)

## Responses

Markdown supported · ⌘↵ to post

No responses yet.
