我有以下测试和验证规则:
if z == 10:
在/** @var \App\Http\Requests\StorePopularClientRequest*/
private $rules;
/** @var \Illuminate\Validation\Validator */
private $validator;
public function __construct($name = null, array $data = array(), $dataName = '') {
parent::__construct($name, $data, $dataName);
$this->createApplication();
}
public function setUp(): void
{
parent::setUp();
$this->adminUser = $this->createUser();
$this->businessUser = $this->createUser('Business');
$this->popularClients = factory(PopularClient::class, 20)->create()->each(function($client) {
static $order = 1;
$client->order = $order;
$client->save();
$order++;
});
$this->validator = app()->get('validator');
$this->rules = (new StorePopularClientRequest())->rules();
}
public function validationProvider()
{
/* WithFaker trait doesn't work in the dataProvider */
$faker = Factory::create(Factory::DEFAULT_LOCALE);
$file = factory(Attachment::class)->create(['category' => 'logo']);
return [
'request_should_pass_when_data_is_provided' => [
'passed' => true,
'data' => [
'name' => $faker->company(),
'description' => $faker->sentence($nbWords = 20, $variableNbWords = true),
'order' => 1,
'file' => [
"id" => $file->id,
"name" => $file->name,
]
]
]
];
}
/**
* @test
* @dataProvider validationProvider
* @param bool $shouldPass
* @param array $mockedRequestData
*/
public function validation_results_as_expected($shouldPass, $mockedRequestData)
{
$this->assertEquals(
$shouldPass,
$this->validate($mockedRequestData)
);
}
protected function validate($mockedRequestData)
{
return $this->validator
->make($mockedRequestData, $this->rules)
->passes();
}
中,我有以下规则
StorePopularClientRequest
除了public function rules()
{
return [
"name" => [
'required','string',
],
"description" => [
'required', 'string', 'min:5',
],
'order' => ['required', 'integer'],
'file' => ['required', 'array'],
'file.id' => ['required', 'exists:attachments,id'],
'file.name' => ['required', 'exists:attachments,name'],
];
}
,exists:attachments, name
以外,所有这些规则均正常运行,因为使用了exists:attachments,id
,因此如果注释RefreshDatabase
可以正常工作。
那么有没有办法在所有测试完成之前不刷新数据库,然后刷新数据库?
现在附件工厂可以工作了,并在RefreshDatabase
表中创建附件数据,但是当我运行测试时,附件表在测试前会刷新,并且附件的存在规则无效