简体   繁体   中英

Laravel PHPUnit: how to test url prefix

I'm learning to build some PHPUnit tests. My app redirects to default language when no prefix is used, for instance: www.myapp.com has 301 redirect to www.myapp.com/en, while www.myapp.com/en has a 200 OK.

Trying to make tests to my Controller, i have the following code

    $response = $this->action('GET', 'HomeController@index');
    $this->assertEquals(200, $response->getStatusCode());

The response is 301 (because of this language prefix check). How can I test these controllers with prefix ? Thanks

Try adding this property to your tests/TestCase.php file.

// The base URL to use while testing the application.
protected $baseUrl = 'http://localhost/en';

From Laravel 5.4 the $baseUrl method doesn't work anymore

You can set a custom root URL dinamycally in your tests with:

\URL::forceRootUrl('http://www.myapp.com/en');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM