我将 PHPUnit 3.5 和 CIUnit 与 PHING 2.4 一起用于单元测试我的 CodeIgniter Controller 功能
问题是当我测试的函数包含 redirect() 函数时,测试用例将停止并且不会继续执行。也没有可用的错误日志。
这可能是什么问题?我必须下载/更新 PHPUnit 特定库吗?
如有任何建议,我们将不胜感激。
谢谢
最佳答案
嗯,这是因为 redirect()
fn exits 在执行重定向后(当您执行重定向以停止进一步的代码执行时,您应该这样做)在等待浏览器重定向时)。
/system/helpers/url_helper.php:
/**
* Header Redirect
*
* Header redirect in two flavors
* For very fine grained control over headers, you could use the Output
* Library's set_header() function.
*
* @access public
* @param string the URL
* @param string the method: location or redirect
* @return string
*/
if ( ! function_exists('redirect'))
{
function redirect($uri = '', $method = 'location', $http_response_code = 302)
{
if ( ! preg_match('#^https?://#i', $uri))
{
$uri = site_url($uri);
}
switch($method)
{
case 'refresh' : header("Refresh:0;url=".$uri);
break;
default : header("Location: ".$uri, TRUE, $http_response_code);
break;
}
exit; <===<<< here is the exit
}
}
“解决”这个问题的唯一方法是在重定向调用之后消除 exit;
。
引用: http://codeigniter.com/user_guide/helpers/url_helper.html
关于unit-testing - 使用 PHPUnit 和 CIUnit 在 CodeIgniter 2 中重定向函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9668455/