I have such files:
/src/Api.php
<?php
namespace src;
class Api {
function apiCall()
{
return 'api_result';
}
}
/tests/_bootstrap.php
<?php
include __DIR__.'/../vendor/autoload.php'; // composer autoload
$kernel = \AspectMock\Kernel::getInstance();
$kernel->init([
'debug' => true,
'includePaths' => [__DIR__.'/../src'],
'cacheDir' => __DIR__ . '/aspectCache'
]);
codeception.yml
paths:
tests: tests
output: tests/_output
data: tests/_data
support: tests/_support
envs: tests/_envs
actor_suffix: Tester
extensions:
enabled:
- Codeception\Extension\RunFailed
settings:
bootstrap: _bootstrap.php
composer.json
{
"require-dev": {
"codeception/aspect-mock": "*",
"codeception/codeception": "^2.3"
},
"autoload": {
"psr-4": {
"": "src/"
}
}
}
index.php
<?php
require_once "vendor/autoload.php";
use src\Api;
$api = new Api();
echo $api->apiCall();
echo 'test';
/tests/acceptance/FirstCest.php
<?php
use AspectMock\Test as test;
use src\Api;
class FirstCest
{
public function frontpageWorks(AcceptanceTester $I)
{
$I->amOnPage('/');
test::double(Api::class, ['apiCall' => 'mock']);
$I->see('mocktest');
}
}
When I load page in browser I see string 'api_resulttest'
Now when I mock the apiCall function, the output should be 'mocktest'.
I run command
php codecept.phar run --steps -d
And test fails, I still see the output 'api_resulttest'.
Why? And do I use it correctly? https://github.com/Codeception/AspectMock here it does not show how to use it in codeception tests.
Or please tell another way - how should I mock api calls in codeception? Thats what I want to do.
I have pushed into bitbucket so you can test the example: https://bitbucket.org/darius_v/codeception_mock/src
Update 2017 10 01 Now removed aspect mock in my newest commits.