I have a container factory which is as follows:
<?php
namespace Database\Factories;
use App\Models\Container;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Arr;
class ContainerFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
$preparationDate = $this->faker->dateTimeBetween('-5 years', '-3 weeks');
.
.
.
return [
.
.
'preparation_date' => $preparationDate,
];
}
}
When I use tinker, this produces an appropriate container.
However, when I use it in a test class method (i.e. not a test but a helper function) like:
namespace Tests\Feature\Samples;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;
class FormValidationTest extends TestCase
{
/* Number of test cases here not shown
*
*/
// This function is a helper for my data provider for my tests.
public function generatePostData(string $path, $value)
{
$container = Container::factory()->create(); // This line gives error
.
.
.
return [
.
.
.
'container' => $container
];
}
}
It throws the error:
The data provider specified for Tests\Feature\Samples\FormValidationTest ::test_user_cant_store_invalid_samples is invalid.
InvalidArgumentException: Unknown formatter "dateTimeBetween"
Edit: The same $container = Container::factory()->create(); works fine within a test method in the same file like
public function test_a_feature() {
$container = Container::factory()->create(); // This works when put in a test
}
How can I create models in a separate function?