Mocking with a Closure

Open
#131 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
php
Domain
testing

Research direction

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.

Written by the indexing model from the issue text.

Description

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?

Dominant language
PHP
Stars
784
Forks
130
PR merge metrics
No merged PRs in 30d

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from Codeception/AspectMock

All issues in Codeception/AspectMock

Similar issues

More PHP issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.