我在测试软件包时有一个关于路由的问题。函数setRoutes在测试文件中创建新路由,如下所示:
class PackageTests extends \Orchestra\Testbench\TestCase {
protected function setRoutes()
{
Route::group([
'prefix' => Package::functionToCall1(),
'before' => 'filter'
], function() {
Route::get('/', function () {
return "hello";
});
});
Route::enableFilters();
}
protected function getEnvironmentSetUp($app)
{
$this->app = $app;
$this->setRoutes();
Config::set('app.url', "http://localhost/" );
}
public function testFunction1()
{
$crawler = $this->call(
'GET',
'http://localhost/'
);
// doing this call, the function on the prefix is called
$this->assertResponseOk();
}
}
在前缀中调用的函数内,functionToCall1()url未成功获取。致URL::current()
的回复" /"并呼叫Request::fullUrl()
返回" http://:"当phpunit被执行但是当他们在浏览器中执行url时返回完整的url。这是包的代码:
class Package
{
function functionToCall1()
{
var_dump(URL::current() ); // returns "/"
var_dump(Request::fullUrl()); // returns "http://:"
// I want them to return 'http://localhost'
}
}
我尝试设置网址Config::set('app.url', "http://localhost/" );
,但它没用。
总结一下,有没有办法在前缀中调用函数并获取测试网址?
谢谢,我真的很感激你的答案:)
答案 0 :(得分:1)
我不得不处理类似的问题。我的解决方案在这里找到: Mocking Laravel's Request::segment method
显然,测试请求外观存在操作顺序问题。
我在构建请求之前尝试使用Request :: segments(),因此从来没有任何段返回。
我想象Request::fullUrl()
也是同样的问题。
这是我的解决方案:
class MyTestClass extends TestCase
{
public function setUp()
{
// No call to parent::setUp()
$this->app = $this->createApplication();
$this->app->request->server->set('REQUEST_URI', '/some/uri');
$this->client = $this->createClient();
$this->app->boot();
}
public function testWhatever()
{
$this->call('GET', '/some/uri');
}
}
这使我可以正确获取请求数据,即使它看起来很糟糕。