Looks like you have a typo in your route. Should it be route('projects.store') ?
Aug 3, 2019
7
Level 2
Unit/Feature Test : test if radio button is checked or not .
Hi there.
I'm trying to do test on a form. I want to test if, there a radio button is not checked, the errors displayed.
Actually. My test fails.
//my database structure:
categories
____________
-id
-name
-categorizable_id
-categorizable_type
projects
_________
-id
-user_id
-title
-thumbnail
-material_id
-content
materials
__________
-id
-name
users
______
-id
-name
-firstname
-email
In my categories table, there are already 6 pre-defined/written categories.
// html code
<div>
@if($errors->has('category'))
<div class="error-box">
<small><i class="fa fa-times" aria-hidden="true"></i> {{$errors->first('category')}}</small>
</div>
@endif
<div class="categories">
@foreach ($categories as $enName => $frName)
<div>
<label for="{{$enName}}">
<input type="radio" id="{{$enName}}" name="category[]" class="category">
<span>{{$frName}}</span>
</label>
</div>
@endforeach
</div>
</div>
//my category factory:
<?php
/* @var $factory \Illuminate\Database\Eloquent\Factory */
use App\Category;
use Faker\Generator as Faker;
$factory->define(Category::class, function (Faker $faker) {
return [
'fr_name' => $faker->word(),
'en_name' => $faker->word(),
'categorizable_id' => function () {
return factory(App\Project::class)->make()->id;
},
'categorizable_type' => function (array $cat) {
return App\Project::find($cat['project_id'])->type;
}
];
});
//my project factory
<?php
/* @var $factory \Illuminate\Database\Eloquent\Factory */
use App\Project;
use Faker\Generator as Faker;
$factory->define(Project::class, function (Faker $faker) {
return [
'user_id' => function () {
return factory(App\User::class)->create()->id;
},
'title' => $faker->word(4),
'thumbnail' =>$faker->image('public/images/',640,480, null, false),
'material_id'=> function () {
return factory(App\Material::class)->create()->id;
},
'content' => $faker->text()
];
});
my test
public function all_fields_are_empty()
{
$user = factory('App\User')->make();
$project = factory('App\Project')->create();
$response = $this->actingAs($user)->post(route('projets.store'),[
'category' => [],
'title' => NULL,
'thumbnail' => NULL,
'material[]' =>NULL,
'content' => NULL
]);
$response->assertSessionHasErrors('category', 'title', 'material[]', 'content');
}
//errors
...........F............ 24 / 24 (100%)
Time: 19.76 seconds, Memory: 28.00 MB
There was 1 failure:
1) Tests\Feature\ProjectCreationTest::all_fields_are_empty
Session missing error: category
Failed asserting that false is true.
/var/www/html/goshr/vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestResponse.php:933
/var/www/html/goshr/tests/Feature/ProjectCreationTest.php:85
/home/audrey/.composer/vendor/phpunit/phpunit/src/TextUI/Command.php:201
/home/audrey/.composer/vendor/phpunit/phpunit/src/TextUI/Command.php:160
FAILURES!
Tests: 24, Assertions: 72, Failures: 1.
Can you enlighthen me please?
Thank by advance.
Level 2
@mikenewbuild I find I found solution for this test (below )
/**
* The field category is missing
*
* @return void
* @test
*/
public function the_field_category_is_missing()
{
$user = factory(User::class)->make();
$projects = factory(Project::class, 3)
->create()
->each(function ($project) {
$project->category()->save(factory(Category::class)->make());
});
foreach($projects as $project){
$response = $this->actingAs($user)->post(route('projets.store'),[
'category' => NULL ,
'user_id' => $project->user_id,
'title' => $project->title,
'thumbnail' =>$project->thumbnail,
'material_id' => $project->material_id,
'content' => $project->content
]);
}
$response->assertStatus(302)->assertSessionHasErrors(['category' => 'Votre projet doit être classé dans une catégorie']);
}
Than you a lot for your help, hoping my solution is the good one.
1 like
Please or to participate in this conversation.