think-testing
think-testing copied to clipboard
makeRequest无法传参数和不支持普通URL模式
如下代码:
<?php
namespace app\index\controller;
use think\Request;
class Index
{
public function getMethod($name){
return $name;
}
public function getMethod2(){
return Request::instance()->param('name');
}
}
使用普通URL模式:index/index/getMethod?name=test 在web端正常。但是在命令行下面,由于$_GET这个变量不会被置值,因此上面两种情况都是失败的。
以上原因会造成单元测试扩展中的makeRequest中的参数传进去,但是没有效果:
<?php
namespace tests;
use Symfony\Component\DomCrawler\Crawler;
//针对Index控制器
class IndexTest extends TestCase
{
public function testGetMethod(){
$this->makeRequest('GET','/index/index/getMethod',array('name','test'))->see('test');
}
}
会导致下面的错误:
- tests\IndexTest::testGetMethod InvalidArgumentException: method param miss:name
原因是Request::create中对param的解析是使用上述的URL:
$queryString = http_build_query($params, '', '&');
而makeRequestUsingForm是基于makeRequest的,因此理论上这个也失效了。
目前我的解决方法是,在Request::create中,手动将param添加到$_GET中。
post put 也都不好使