单元测试:使用 mocking/stubbing 的正确术语
Unit test: using the proper terminology for mocking/stubbing
在对我的项目系统架构进行根本性更改后,我发现自己处于需要创建“fake”实现的情况,以便测试一些曾经是public 如下:
/**
* 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
class 以前直接在控制器中调用,但现在是控制器扩展的抽象 class 我的单元测试无法再访问方法。
我的新会话包装器 class 中也有这样的方法,只能在非常特定的上下文中使用,为此我确实需要创建假页面实现来测试它们。
/**
* 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;
}
即使我看了一些文章,我仍然不清楚那些假 classes 应该被视为“stub”还是“ mock”(甚至是“fake”、“dummy”等等)。
我真的需要使用正确的术语,因为我的老板希望我(在不久的将来)将我的大部分工作量委派给海外开发人员。
为了不言自明,您如何称呼那些仅为测试目的而创建的虚假 class 实现?
在对我的项目系统架构进行根本性更改后,我发现自己处于需要创建“fake”实现的情况,以便测试一些曾经是public 如下:
/**
* 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
class 以前直接在控制器中调用,但现在是控制器扩展的抽象 class 我的单元测试无法再访问方法。
我的新会话包装器 class 中也有这样的方法,只能在非常特定的上下文中使用,为此我确实需要创建假页面实现来测试它们。
/**
* 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;
}
即使我看了一些文章,我仍然不清楚那些假 classes 应该被视为“stub”还是“ mock”(甚至是“fake”、“dummy”等等)。
我真的需要使用正确的术语,因为我的老板希望我(在不久的将来)将我的大部分工作量委派给海外开发人员。
为了不言自明,您如何称呼那些仅为测试目的而创建的虚假 class 实现?