4

I have an issue when I run tests using DatabaseTransactions. In my Products table, I have all Products where the stock_count = -99.

Here is my code

TestCase.php (all the tests extend this class)

use DatabaseTransactions;

protected $connectionsToTransact = ["mysql"];

public function setUp()
{
   parent::setUp();
   MimProduct::query()->update(['stock_count'=>100]);
   ...
}

Then in one of my test, I dd the same query, one using DB Facade the other one using the actual model.

 dd(DB::table("mim.products")->first(),MimProduct::all()->first());

and the output is

enter image description here

My conclusions so far are:

  • Using the DB facade doesn’t work with the DB Transaction trait; it cannot use the “in-memory” transactions, and so it will work off the “live” DB.
  • Whereas a model has access to memory.

Am I correct or there is a way to overcome this issue?

Karl Hill
  • 12,937
  • 5
  • 58
  • 95
Alessandro.Vegna
  • 1,262
  • 10
  • 19

0 Answers0