2

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

Moppo
  • 18,797
  • 5
  • 65
  • 64
Diogo Mendonça
  • 903
  • 2
  • 12
  • 29

2 Answers2

3

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

// The base URL to use while testing the application.
protected $baseUrl = 'http://localhost/en';
BrokenBinary
  • 7,731
  • 3
  • 43
  • 54
2

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');
Moppo
  • 18,797
  • 5
  • 65
  • 64