Mocking with a Closure
vinkaga opened this issue · 0 comments
vinkaga commented
For the following class
class Demo {
public function aFunc() {
return 'initialFunc';
}
}
I have written the following test that mocks the class replacing aFunc
with a closure as follows
class DemoTest extends PHPUnit_Framework_TestCase {
public function testWithClosure() {
\AspectMock\Test::double(Demo::class, [
'aFunc' => function($a) { return $a.$a; },
]);
$mockDemo = new Demo();
$this->assertEquals('abcabc', $mockDemo->aFunc('abc'));
}
}
When I run it, I get the following error
$ ../vendor/bin/phpunit .
PHPUnit 4.8.35 by Sebastian Bergmann and contributors.
E
Time: 222 ms, Memory: 7.25MB
There was 1 error:
1) DemoTest::testWithClosure
Missing argument 1 for Demo::{closure}()
dev/ez-mock/test/DemoTest.php:10
dev/ez-mock/vendor/codeception/aspect-mock/src/AspectMock/Core/Mocker.php:154
dev/ez-mock/vendor/codeception/aspect-mock/src/AspectMock/Core/Mocker.php:79
dev/ez-mock/vendor/codeception/aspect-mock/src/AspectMock/Core/Mocker.php:28
dev/ez-mock/vendor/codeception/aspect-mock/src/AspectMock/Intercept/before_mock.php:3
dev/ez-mock/test/Demo.php:4
dev/ez-mock/test/DemoTest.php:13
FAILURES!
Tests: 1, Assertions: 0, Errors: 1.
What am I doing wrong?