【发布时间】:2014-12-10 17:31:32
【问题描述】:
我在测试包时有一个关于路由的问题。函数 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() 的调用在执行phpunit 时返回“http://:”,但在用于在浏览器中执行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/" );,但没用。
总结一下,有没有办法在前缀中调用函数,获取测试url?
谢谢,非常感谢您的回答:)
【问题讨论】:
标签: unit-testing testing laravel laravel-routing