如何将 Laravel 测试设置为转到站点名称而不是本地主机

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33547952/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 12:39:05  来源:igfitidea点击:

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

phplaraveltestingphpunitintegration-testing

提问by Rockwell Rice

I am trying to write some tests for an application. I have the server set up on MAMP going to dev.myappnamehere.com.

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

When I run a test (based off of Laracasts Integrated) it fails because it is looking for the route

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

 http://localhost/p/profile

But what it needs to go to is

但它需要去的是

 http://dev.myappnamehere/p/profile

How can I change that so it does not default to looking for the localhost and instead goes to the correct path?

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

I attempted to change this in the test but got nowhere and I was unable to located an answer through googling.

我试图在测试中改变这一点,但一无所获,我无法通过谷歌搜索找到答案。

 <?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('[email protected]', 'email')
        ->type('password', 'password')
        ->press('Login')
        ->seePageIs('/p/profile');
  }

}

采纳答案by Rockwell Rice

So shortly after I asked I stumbled on the answer. In

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

 LaravelTestCase.php 

there is a function called

有一个函数叫做

  baseUrl() 

which sets the url it looks for. Once I changed that it looked in the correct spot. That file was part of the laracast testing that I loaded in.

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

回答by Moppo

From Laravel 5.4 the $baseUrlmethod doesn't seem to work anymore

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

Also, trying to set the url dinamically with \Config:set('app.url', 'http://dev.myappnamehere')doesn't work either, as it seems that Laravel caches the root url

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

A way to set a custom root url is:

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

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

回答by Dallin

For Laravel 5, In the testsdirectory there should be a file called TestCase.php. In that file is a property $baseUrl. Update the value to your desired url. For example change

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

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

to

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

回答by user2161402

Change the APP_URL in your .env file from localhost to your desired URL.

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

Ex: APP_URL=http://myproject.test

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

回答by Yevgeniy Afanasyev

Most likely you need to change a domain root in your existing request().

您很可能需要更改现有request().

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

test

测试

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

http://homestead.test

http://homestead.test

回答by taisa007

The below instead

下面代替

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