我正在尝试为应用程序编写一些测试。我已经在MAMP上设置了服务器,可以访问dev.myappnamehere.com。
当我运行一个测试(基于Laracasts Integrated)时,它失败了,因为它正在寻找路线
http://localhost/p/profile
但它需要去
http://dev.myappnamehere/p/profile
我如何更改它,使其不默认为查找localhost,而是转到正确的路径?
我试图在测试中改变这一点,但一无所获,我无法通过谷歌搜索找到答案。
<?php
use Laracasts'Integrated'Extensions'Laravel as IntegrationTest;
use Laracests'TestDummy'Factory as TestDummy;
class ExampleTest extends TestCase {
/**
* A basic functional test example.
*
* @return void
*/
public function testBasicExample()
{
$this->visit('/')
->see('Login')
->type('example@example.com', 'email')
->type('password', 'password')
->press('Login')
->seePageIs('/p/profile');
}
}
从Laravel 5.4开始,$baseUrl
方法似乎不再适用于
此外,尝试用'Config:set('app.url', 'http://dev.myappnamehere')
普通设置url也不起作用,因为Laravel似乎缓存了根url
设置自定义根url的方法是:
'URL::forceRootUrl('http://dev.myappnamehere');
所以在我询问后不久,我偶然发现了答案。在中
LaravelTestCase.php
有一个叫做的函数
baseUrl()
它设置了它要查找的url。一旦我改变了,它看起来就在正确的位置。该文件是我加载到的laracast测试的一部分。
对于Laravel 5,在tests
目录中应该有一个名为TestCase.php
的文件。该文件中有一个属性$baseUrl
。将值更新为所需的url。例如更改
protected $baseUrl = 'http://localhost';
至
protected $baseUrl = 'http://dev.myappnamehere';
很可能您需要更改现有request()
中的域根。
$domain = 'homestead.test';
request()->headers->set('HOST', $domain);
测试
'Log::debug(request()->root());
http://homestead.test
将.env文件中的APP_URL从localhost更改为所需的URL。
例如:APP_URL=http://myproject.test
下面是
$this->visit('http://dev.myappnamehere')