Unit Testing my app's local REST API calls, but using the testing sqlite DB ...
Hi all,
I'm building unit tests for my API service, using a sqlite in-memory database - this has worked well for in-application method and DB testing.
However, I do need to test my API over HTTP. When I call a $this->getJson in my Unit Test class, the request is being handled outside of the test environment, e.g. no longer on the test database (since I assume Laravel can't possibly tell that the incoming HTTP request from my unit test is a test, and therefore, can't know that it should connect to the in-memory database instead of the one defined in .env)
What's the best method to have Unit Test-driven HTTP calls internally be treated as a test and therefore, using my sqlite DB?
Executing http requests via your testcase will run them not over http but from within the test environment :) This is of course whenever you're using the $this object to create the http calls.
Whenever you start using things like guzzle to do the request inside your test this obviously is not going to work :)
The way this works is that $this->getJson (which under the hood becomes a simple $this->get / post etc.) simply creates an HttpKernel and injects the build request into it as if it was an incoming HTTP request :) to find out more simply read the source code from the Concerns\MakesHttpRequests trait. This is where the magic happens :) the behaviour i mentioned happens in the call() method of the trait. This trait is put on the TestCase class by default within Laravel.