如何设置laravel测试以转到站点名称而不是localhost

时间:2015-11-05 15:02:38

标签: php laravel testing phpunit integration-testing

我正在尝试为应用程序编写一些测试。我将服务器设置在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');
  }

}

6 个答案:

答案 0 :(得分:6)

在我问我偶然发现答案后不久。在

 LaravelTestCase.php 

有一个名为

的函数
  baseUrl() 

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

答案 1 :(得分:6)

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

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

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

答案 2 :(得分:6)

从Laravel 5.4开始,$baseUrl方法似乎不再适用

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

设置自定义根网址的方法是:

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

答案 3 :(得分:0)

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

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

答案 4 :(得分:0)

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

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

测试

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

http://homestead.test

答案 5 :(得分:-3)

以下代替

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