Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to return values from mocked method using test::spec #147

Open
bartdorsey opened this issue Feb 23, 2018 · 4 comments
Open

How to return values from mocked method using test::spec #147

bartdorsey opened this issue Feb 23, 2018 · 4 comments

Comments

@bartdorsey
Copy link

Is it possible to return fake values from mocked methods using an object created with test::spec ? I've tried this and it never seems to call my fake method:

$fakeValue = '1';
$mock = test::spec('FakeClass', [
    'fakeMethod' => function () {
         return $fakeValue;
    }
])->construct();

Then when I call $mock->fakeMethod() it's not returning my fakeValue;

Is this just supported or am I doing something wrong?

@adrian-enspired
Copy link

your $fakeValue is out of scope.

    'fakeMethod' => function () use ($fakeValue) {
         return $fakeValue;
    }

@bartdorsey
Copy link
Author

Doh. I'll have to try this then, thanks.

@bartdorsey
Copy link
Author

I'm still having problems with this. Here's my latest example.

   $mockDB = test::spec('DB', [
        'queryRow' => function () {
             codecept_debug('Inside Fake Function');
             return 1;
         }
   ])->construct();
   codecept_debug($mockDB->queryRow());

This should return a 1 and codecept_debug twice.

It only logs once and prints this:

AspectMock\Proxy\Anything Object
  (
      [className:AspectMock\Proxy\Anything:private] => DB
  )

I've also tried putting the array of methods to override inside the construct() call. It does the same thing.

@bartdorsey bartdorsey reopened this Mar 27, 2018
@bartdorsey
Copy link
Author

public function testAspectMock()
        {
            $mockThing = test::spec('FakeClass', [
                'fakeMethod' => function () {
                    return "Hello World";
                }
            ])->construct();
            $this->assertEquals($mockThing->fakeMethod(), "Hello World");
        }

Here's a test I wrote in my codeception unit test that fails with this:

Failed asserting that 'Hello World' matches expected AspectMock\Proxy\Anything Object &000000006b16ca0d000055bfa042f94a (
    'className' => 'FakeClass'
).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants