2017-09-23 66 views
0

我有这样的文件:看点模拟不超载方法codeception

/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'); 
    } 
} 

当我在浏览器中加载页面我看到字符串 'api_resulttest'

现在,当我嘲笑apiCall函数,输出应该是'mocktest'。

我运行命令

php codecept.phar run --steps -d 

和测试失败,我还是看到了输出 'api_resulttest'。

为什么?我能正确使用它吗? https://github.com/Codeception/AspectMock这里它不显示如何在代码测试中使用它。

或者请告诉别的方法 - 我应该如何在代码中模拟api调用?那就是我想要做的。

我已经推入到位桶,所以你可以测试的例子: https://bitbucket.org/darius_v/codeception_mock/src

更新2017年10 01 现在去除方面模仿我的最新提交。

回答

0

PhpBrowser通过HTTP向您的网站提出请求,因此测试代码中设置的模拟对应用程序代码没有影响。

如果您正在使用框架模块,则模拟工作在单元测试和功能测试中。

+0

好吧,但如果我没有像这个测试应用程序框架,仍然应该有可能嘲笑,不应该吗? –

+0

是的,如果您使用常规单元测试而不是使用PhpBrowser。 – Naktibalda