Testing a model with connection with an external table
Hi y'all,
I have an issue with a test case where I'm testing with a model that is connected to a DB that on Azure, when I create a factory for the model and run factory create, it tries to create the record on that DB.
Even when I try to mock the model class it just throws errors
sample model
class Yatcher extends Model
{
use HasFactory;
protected $guarded = [];
protected $connection = "yatch";
protected $table = "miles";
}
Typically you'll set up a different database connection for testing - you can define the DB_CONNECTION and DB_DATABASE in the phpunit.xml file.
As for your actual question, the model you posted doesn't tell us much and it's hard to help diagnose errors if we don't know what they are. What errors are you seeing?
@maxxd
We actually have DB_CONNECTION and DB_DATABASE in our phpunit.xml file. The major issue is that when we try to use Yatcher::factory()->create() it tries to create the record on the model's connection which might a test DB
@caleb65 - I realize this is old, but did you get your problem solved? I think there's a word or two missing in your last response so I'm not entirely sure what you're describing.