How to return values from mocked method using test::spec
bartdorsey opened this issue · 4 comments
bartdorsey commented
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 commented
your $fakeValue
is out of scope.
'fakeMethod' => function () use ($fakeValue) {
return $fakeValue;
}
bartdorsey commented
Doh. I'll have to try this then, thanks.
bartdorsey commented
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 commented
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'
).