gpt4 book ai didi

php - 单元测试: using the proper terminology for mocking/stubbing

转载 作者:行者123 更新时间:2023-12-03 10:07:14 24 4
gpt4 key购买 nike

在对项目系统体系结构进行了根本性更改之后,我发现自己处于一种需要创建“伪造”实现以测试某些以前是公开的功能的情况,如下所示:

/**
* Display the template linked to the page.
*
* @param $newSmarty Smarty object to use to display the template.
*
* @param $parameters associative Array containing the values to pass to the template.
* The key is the name of the variable in the template and the value is the value of the variable.
*
* @param $account child class in the AccountManager hierarchy
*
* @param $partialview String name of the partial view we are working on
*/
protected function displayPageTemplateSmarty(Smarty &$newSmarty, array $parameters = array(), AccountManager $account = NULL, string $partialview = "")
{
$this->smarty = $newSmarty;

if (is_file(
realpath(dirname(__FILE__)) . "/../../" .
Session::getInstance()->getCurrentDomain() . "/view/" . (
!empty($partialview) ?
"partial_view/" . $partialview :
str_replace(
array(".html", "/"),
array(".tpl", ""),
Session::getInstance()->getActivePage()
)
)
)) {

$this->smarty->assign(
'activeLanguage',
Session::getInstance()->getActiveLanguage()
);

$this->smarty->assign('domain', Session::getInstance()->getCurrentDomain());

$this->smarty->assign(
'languages',
Languagecontroller::$supportedLanguages
);

$this->smarty->assign(
'title',
Languagecontroller::getFieldTranslation('PAGE_TITLE', '')
);

$this->smarty->assign_by_ref('PageController', $this);

$htmlTagBuilder = HTMLTagBuilder::getInstance();

$languageController = LanguageController::getInstance();

$this->smarty->assign_by_ref('htmlTagBuilder', $htmlTagBuilder);
$this->smarty->assign_by_ref('languageController', $languageController);

if (!is_null($account)) {

$this->smarty->assign_by_ref('userAccount', $account);
}

if (!is_null($this->menuGenerator)) {

$this->smarty->assign_by_ref('menuGenerator', $this->menuGenerator);
}

foreach ($parameters as $key => $value) {

$this->smarty->assign($key, $value);
}

$this->smarty->display((!empty($partialview) ?
"partial_view/" . $partialview :
str_replace(
array(".html", "/"),
array(".tpl", ""),
Session::getInstance()->getActivePage()
)
));
}
}

在这种情况下,曾经在 Controller 中直接调用 PageController类,但是现在它是由 Controller 扩展的抽象类,并且我的单元测试无法再访问该方法。

我的新 session 包装器类中也有类似这种方法,这些方法只能在非常特定的上下文中使用,我确实需要为此创建伪造的页面实现来对其进行测试。
/**
* Add or update an entry to the page session array.
*
* Note: can only be updated by the PageController.
*
* @param $key String Key in the session array.
* Will not be added if the key is not a string.
*
* @param $value The value to be added to the session array.
*
* @return Boolean
*/
public function updatePageSession(string $key, $value)
{
$trace = debug_backtrace();

$updated = false;

if (isset($trace[1]) and
isset($trace[1]['class']) and
$trace[1]['class'] === 'PageController'
) {

$this->pageSession[$key] = $value;

$updated = true;
}

return $updated;
}

即使我读了几篇文章,我仍然不清楚那些假类是否应视为“ stub ”或“模拟”(甚至是“假”,“假”等)。

我真的需要使用适当的术语,因为老板希望我(在不久的将来)将大部分工作分配给海外开发人员。

您如何称呼那些仅出于测试目的而创建的虚假类实现,以便于自我解释?

最佳答案

杰拉德·梅萨罗斯(Gerard Meszaros)解释了假人, stub , spy , mock 和假冒here的术语。

您可以从PHP世界here中找到示例。

关于php - 单元测试: using the proper terminology for mocking/stubbing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42007627/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com