I am trying to write some tests for an application. I have the server set up on MAMP going to dev.myappnamehere.com.
When I run a test (based off of Laracasts Integrated) it fails because it is looking for the route
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');
}
}
You need to make sure XAMPP, WAMP etc is listening for traffic to that domain and then point your local hosts file at your server which is usually 127.0. 0.1 So for example using XAMPP you would add an addition to your httpd-vhosts. config file mapping the domain to the public Laravel folder.
Unit tests are tests that focus on a very small, isolated portion of your code. In fact, most unit tests probably focus on a single method. Tests within your "Unit" test directory do not boot your Laravel application and therefore are unable to access your application's database or other framework services.
Most likely you need to change a domain root in your existing request()
.
$domain = 'homestead.test';
request()->headers->set('HOST', $domain);
test
\Log::debug(request()->root());
http://homestead.test
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.
From Laravel 5.4 the $baseUrl
method doesn't seem to work anymore
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
A way to set a custom root url is:
\URL::forceRootUrl('http://dev.myappnamehere');
For Laravel 5,
In the tests
directory 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
protected $baseUrl = 'http://localhost';
to
protected $baseUrl = 'http://dev.myappnamehere';
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With