mediacie's avatar

Models using Searchable not usable in unit test

When writing unit tests, using models with the Searchable treat are failing during booting of the model with the message A facade root has not been set.. The second time the model is used, the error doesn't occur again (probably because the model is booted earlier). Disabling the Searchable treat on the model or using CreatesApplication for the tests solves the problem. However, I think it should be possible to use searchable models without booting the application.

During the tests the env SCOUT_DRIVER is set to "collection"

Steps To Reproduce:

  1. Create a model with a factory and use the Searchable treat for the model.
  2. Use the model in a unit test:
use App\Models\SearchableModel;

// First test
it('should do something', function () {
    $seachable_model = SearchableModel::factory()->make(); // Fails: "A facade root has not been set."

    $this->assertEqual('foo', $searchable_model->bar);
});

// Second test
it('should do something', function () {
    $seachable_model = SearchableModel::factory()->make(); // Not failing

    $this->assertEqual('foo', $searchable_model->bar);
});
0 likes
0 replies

Please or to participate in this conversation.