Codeception / Codeception/AspectMock
Mocking with a Closure
- Langage dominant
- PHP
- Étoiles
- 784
- Forks
- 132
- Métriques de merge des PR
- Aucune PR mergée en 30 j
Description
For the following class
```PHP
class Demo {
public function aFunc() {
return 'initialFunc';
}
}
```
I have written the following test that mocks the class replacing `aFunc` with a closure as follows
```PHP
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
```SH
$ ../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?
Guide de contribution
Aucun guide de contribution indexé pour ce dépôt
Piste de recherche
Reproduce the failure in dev/ez-mock/test/DemoTest.php using the Demo class from dev/ez-mock/test/Demo.php, then trace the call through vendor/codeception/aspect-mock/src/AspectMock/Core/Mocker.php. Compare the closure mock invocation with the test's expected abcabc result; done means the focused PHPUnit test passes without the missing-argument error.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Évaluation
- Stack technique
- php
- Domaine
- testing
- Type d'issue
- Bug
- Difficulté
- 3/5
- Temps estimé
- 1-2 jours
- Activité
- À l'abandon
- Clarté
- Plutôt claire
- Accessibilité débutants
- 35/100