Php Laravel 5.6:AssertSessionHas错误导致;完整性约束冲突:19非空约束失败“;错误
我正在尝试设置一个测试,检查不完整的表单是否无法提交到数据库,并且我不断遇到问题,目前每次运行phpunit时,我都会得到以下结果: 我的测试设置如下:Php Laravel 5.6:AssertSessionHas错误导致;完整性约束冲突:19非空约束失败“;错误,php,laravel,testing,laravel-5.6,laravel-testing,Php,Laravel,Testing,Laravel 5.6,Laravel Testing,我正在尝试设置一个测试,检查不完整的表单是否无法提交到数据库,并且我不断遇到问题,目前每次运行phpunit时,我都会得到以下结果: 我的测试设置如下: public function test_incomplete_form_cannot_be_submitted() { $user = factory(User::class)->states('confirmed', 'normaluser')->create([ 'id' => '1', ]);
public function test_incomplete_form_cannot_be_submitted()
{
$user = factory(User::class)->states('confirmed', 'normaluser')->create([
'id' => '1',
]);
$form = factory(Form::class)->states('incomplete')->create(['status' => 'submitted', 'user_id' => '1', 'id' => '5',]);
$this->actingAs($user)->post(route('forms.store'), $form->toArray());
$this->assertSessionHasErrors(['company_name', 'form_info', 'uploads', 'state']);
}
设置不完整表格的工厂设置如下:
$factory->state(App\Form::class, 'incomplete', function (Faker $faker) {
return [
'company_name' => null,
'form_info' => [
'contact_fname' => null,
'contact_lname' => null,
'address1' => null,
'dist_city' => null,
'zip_code' => null,
'multiple_locations' => null,
'dist_phone' => null,
'dist_email' => null,
'company_website' => null,
'additional_company_info' => null,
'company_ownership_structure' => null,
'business_manager' => [
'0' => [
'title' => null,
'name' => null,
'phone' => null,
'email' => null,
],
],
'business_planner' => [
'0' => [
'title' => null,
'name' => null,
'phone' => null,
'email' => null,
],
],
'sales_rep_compensation' => null,
'midlevel_manager_compensation' => null,
'sales_state' => null,
'county' => [
'0' => null,
],
'split_county' => null,
'split_county_details' => null,
'other_brands_represented' => [
'0' => null,
],
'other_brands_in_territory' => null,
'company_sales_approach' => null,
'vision_for_us' => null,
'who_purchases_the_beer' => null,
'anticipated_spending_details' => null,
'what_other_products' => null,
'chain_retail_coverage' => null,
'total_size' => null,
'last_full_year_sales_for_craft' => null,
'other_breweries_pursue' => null,
'describe_packaged_beer_space' => null,
'describe_cooler_space' => null,
'firkin_handling_procedures' => null,
'secure_storage' => null,
'describe_recoup_area' => null,
'describe_trucks' => null,
'pos_storage_areas' => null,
'quality_policies' => null,
'product_rotation_systems' => null,
'draft_department_capabilities' => null,
'channel_focus' => null,
'channel_focus_details' => null,
'in_house_signs' => null,
'sign_printers' => null,
'sign_shop_employees' => null,
'other_ownership' => null,
'other_ownership_details' => null,
],
'state' => 'Alabama',
'uploads' => [
'top_25_accounts' => null,
'first_year_sales_estimate' => null,
'packaging_space_images' => [
'0' => null,
],
'storage_images' => [
'0' => null,
],
'marketing_signage' => [
'0' => null,
],
],
];
});
我已经尝试将测试从factory…->create更改为factory…->make,修改assertSessionHasErrors以只检查['uploads.top_25_accounts',并使$form只是一个空数组,但似乎没有任何效果 问题1:
public function test_incomplete_form_cannot_be_submitted()
{
$user = factory(User::class)->states('confirmed', 'normaluser')->create([
'id' => '1',
]);
$form = factory(Form::class)->states('incomplete')->make([
'status' => 'submitted',
'user_id' => '1',
'id' => '5',
]);
$response = $this->actingAs($user)->post(route('forms.store'), $form->toArray());
$response->assertSessionHasErrors(['company_name', 'form_info', 'uploads', 'state']);
}
由于工厂中的create()
试图在数据库中创建一个新条目,但company\u name
不能为NULL
,因此无法到达您的断言,并引发异常
解决方案:
public function test_incomplete_form_cannot_be_submitted()
{
$user = factory(User::class)->states('confirmed', 'normaluser')->create([
'id' => '1',
]);
$form = factory(Form::class)->states('incomplete')->make([
'status' => 'submitted',
'user_id' => '1',
'id' => '5',
]);
$response = $this->actingAs($user)->post(route('forms.store'), $form->toArray());
$response->assertSessionHasErrors(['company_name', 'form_info', 'uploads', 'state']);
}
将create()
更改为make()
问题2:
public function test_incomplete_form_cannot_be_submitted()
{
$user = factory(User::class)->states('confirmed', 'normaluser')->create([
'id' => '1',
]);
$form = factory(Form::class)->states('incomplete')->make([
'status' => 'submitted',
'user_id' => '1',
'id' => '5',
]);
$response = $this->actingAs($user)->post(route('forms.store'), $form->toArray());
$response->assertSessionHasErrors(['company_name', 'form_info', 'uploads', 'state']);
}
您必须对响应对象调用assertSessionHasErrors()
<代码>$this->assertSessionHasErrors)不存在
解决方案:
public function test_incomplete_form_cannot_be_submitted()
{
$user = factory(User::class)->states('confirmed', 'normaluser')->create([
'id' => '1',
]);
$form = factory(Form::class)->states('incomplete')->make([
'status' => 'submitted',
'user_id' => '1',
'id' => '5',
]);
$response = $this->actingAs($user)->post(route('forms.store'), $form->toArray());
$response->assertSessionHasErrors(['company_name', 'form_info', 'uploads', 'state']);
}
您是否在
工厂(表单::类)->states('completed')->make(…)
中收到相同的错误?看起来工厂正试图在数据库中创建一个新条目,但是company\u name
不能为NULL并引发异常,因此您的断言永远不会到达。奇怪的是,现在测试只会给我一条错误消息“error:Call to undefined method Tests\Feature\FormTest::assertSessionHasErrors()”我必须指定上传部分(uploads.top_25_accounts),但除此之外,这也成功了,谢谢!