如何设置 laravel 测试以转到站点名称而不是本地主机

How do I set laravel test to go to site name instead of localhost

我正在尝试为应用程序编写一些测试。我在 MAMP 上设置了服务器去 dev.myappnamehere.com.

当我 运行 测试(基于 Laracasts Integrated)时,它失败了,因为它正在寻找路线

 http://localhost/p/profile

但它需要去的是

 http://dev.myappnamehere/p/profile

我该如何更改它,使其不会默认寻找本地主机而是转到正确的路径?

我试图在测试中更改此设置,但一无所获,我无法通过谷歌搜索找到答案。

 <?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');
  }

}

所以在我问完后不久我偶然发现了答案。在

 LaravelTestCase.php 

有一个函数叫做

  baseUrl() 

设置它寻找的 url。一旦我改变它看起来在正确的位置。该文件是我加载的 laracast 测试的一部分。

以下改为

$this->visit('http://dev.myappnamehere')

对于Laravel 5, 在 tests 目录中应该有一个名为 TestCase.php 的文件。在该文件中有一个 属性 $baseUrl。将值更新为您想要的 url。例如更改

protected $baseUrl = 'http://localhost';

protected $baseUrl = 'http://dev.myappnamehere';

从 Laravel 5.4 开始,$baseUrl 方法似乎不再有效

此外,尝试用 \Config:set('app.url', 'http://dev.myappnamehere') 动态设置 url 也不起作用,因为 Laravel 似乎缓存了根 url

设置自定义根 url 的方法是:

\URL::forceRootUrl('http://dev.myappnamehere');

将 .env 文件中的 APP_URL 从 localhost 更改为所需的 URL。

例如: APP_URL=http://myproject.test

您很可能需要更改现有 request() 中的域根。

$domain = 'homestead.test';
request()->headers->set('HOST', $domain);

测试

\Log::debug(request()->root());

http://homestead.test