在我的项目系统架构发生根本性变化后,我发现自己处于需要创建" 假"实现,以便测试一些过去公开的功能,如下所示:
/**
* 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()
)
));
}
}
在这种情况下,PageController
类曾经直接在控制器中调用,但现在是控制器扩展的抽象类,我的单元测试无法再访问该方法。
我的新会话包装器类中也有类似这样的方法,只能在非常特定的上下文中使用,我真的需要创建假页面实现来测试它们。
/**
* 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 "或者" 模拟" (甚至" 假"," 虚拟"依此类推)。
我真的需要使用正确的术语,因为我的老板期待我(在不久的将来)将我的大部分工作量委托给海外开发人员。
为了不言自明,您如何称为仅为测试目的而创建的假类实现?