Home>
Problems i am experiencing

I am currently creating test code, but I am making it according to the validation contents ofrules ()of validation, but the test result is not what I expected is.
falsepasses, but alltrueare played

Append

I thought that the reason might berequired.
Once you try to test withrequiredexcept for one rule, only the code withrequiredsucceeds and all other code fails. As a result, the test code is one line at a time, and all the validation rules are called, so there is a possibility that an error occurs because it is not in the rule.

Applicable source code
// Validation rule description file
<? php
namespace App \ Http \ Requests;
use Illuminate \ Foundation \ Http \ FormRequest;
class ArticleCreateFormRequest extends FormRequest
{
    / **
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     * /
    public function authorize ()
    {
        return true;
    }
    / **
     * Get the validation rules that apply to the request.
     *
     * @return array
     * /
    public function rules ()
    {
        return [
            'articleTitle' =>'required | string | max: 255',
            'articleDescription' =>'required | string | max: 255',
            'postDate' =>'required | date',
        ];
    }
... .. abbreviated ~
// Test code body
<? php
namespace Tests \ Unit;
use Tests \ TestCase;
use Illuminate \ Foundation \ Testing \ WithFaker;
use Illuminate \ Foundation \ Testing \ DatabaseTransactions;
use Illuminate \ Http \ UploadedFile;
use Illuminate \ Support \ Facades \ Storage;
use Illuminate \ Support \ Facades \ DB;
use Illuminate \ Support \ Facades \ Log;
use Illuminate \ Support \ Facades \ Validator;
use App \ Article;
use App \ Http \ Requests \ ArticleCreateFormRequest;
use Tests \ Unit \ UserLoginTest;
use Carbon \ Carbon;
use Auth;
use Faker \ Generator as Faker;

class ArticleTest extends TestCase
{
    use DatabaseTransactions;
    / **
     * @dataProvider dataProviderNewArticle
     * @group exa
     * /
    public function testExample ($dataList, $expect)
    {
        $request = new ArticleCreateFormRequest ();
        // Get the rules defined in the form request
        $rules = $request->rules ();
        // Get validator instance in Validator facade and pass input information and validation rules as arguments
        $validator = Validator :: make ($dataList, $rules);
        // returns true if the input information satisfies the validation rule, false otherwise
        $result = $validator->passes ();
        // Compare expected value ($expect) and result ($result)
        $this->assertEquals ($expect, $result);
    }
// dataProvider (this test code)
    public function dataProviderNewArticle () {
        $this->createApplication ();
        $faker = \ Faker \ Factory :: create ();
        return [
            [['articleTitle' =>$faker->sentence ()], true],
            [['articleTitle' =>''], false],
            [['articleTitle' =>str_repeat ('a', 256)], false],
            [['articleDescription' =>$faker->sentence ()], true],
            [['articleDescription' =>''], false],
            [['articleDescription' =>str_repeat ('a', 256)], false],
            [['postDate' =>$faker->date ()], true],
            [['postDate' =>''], false],[['postDate' =>str_repeat ('a', 256)], false],
        ];
    }
}
Tried

I tried to executerules ()in the Request file side, and it went well

<? php
namespace App \ Http \ Requests;
use Illuminate \ Foundation \ Http \ FormRequest;
class ArticleCreateFormRequest extends FormRequest
{
    / **
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     * /
    public function authorize ()
    {
        return true;
    }
    / **
     * Get the validation rules that apply to the request.
     *
     * @return array
     * /
    public function rules ()
    {
        return [
       //change point
            'articleTitle' =>'required | string | max: 255',
        ];
    }
... .. abbreviated ~


I passed this code and passed the test

<? php
namespace Tests \ Unit;
use Tests \ TestCase;
use Illuminate \ Foundation \ Testing \ WithFaker;
use Illuminate \ Foundation \ Testing \ DatabaseTransactions;
use Illuminate \ Http \ UploadedFile;
use Illuminate \ Support \ Facades \ Storage;
use Illuminate \ Support \ Facades \ DB;
use Illuminate \ Support \ Facades \ Log;
use Illuminate \ Support \ Facades \ Validator;
use App \ Article;
use App \ Http \ Requests \ ArticleCreateFormRequest;
use Tests \ Unit \ UserLoginTest;
use Carbon \ Carbon;
use Auth;
use Faker \ Generator as Faker;

class ArticleTest extends TestCase
{
    use DatabaseTransactions;
    / **
     * @dataProvider dataProviderNewArticle
     * @group exa
     * /
    public function testExample ($dataList, $expect)
    {
        $request = new ArticleCreateFormRequest ();
        // Get the rules defined in the form request
        $rules = $request->rules ();
        // Get validator instance in Validator facade and pass input information and validation rules as arguments
        $validator = Validator :: make ($dataList, $rules);
        // returns true if the input information satisfies the validation rule, false otherwise
        $result = $validator->passes ();
        // Compare expected value ($expect) and result ($result)
        $this->assertEquals ($expect, $result);
    }
// dataProvider (this test code)
    public function dataProviderNewArticle () {
        $this->createApplication ();
        $faker = \ Faker \ Factory :: create ();
        return [
       //change point
            [['articleTitle' =>$faker->sentence ()], true],
            [['articleTitle' =>''], false],
            [['articleTitle' =>str_repeat ('a', 256)], false],
        ];
    }
}
Reference site

https://qiita.com/n_mogi/items/57a946205df2a69889c2

  • Answer # 1

    Since dataProvider was passing data one line at a time, I was able to change the writing method and pass the test.
    I thought that I was able to do both normal processing and abnormal processing in my head at the same time, but I think that I made this mistake.

    Before change

    public function dataProviderNewArticle () {
            $this->createApplication ();
            $faker = \ Faker \ Factory :: create ();
            return [
                [['articleTitle' =>$faker->sentence ()], true],
                [['articleTitle' =>''], false],
                [['articleTitle' =>str_repeat ('a', 256)], false],
                [['articleDescription' =>$faker->sentence ()], true],
                [['articleDescription' =>''], false],
                [['articleDescription' =>str_repeat ('a', 256)], false],
                [['postDate' =>$faker->date ()], true],
                [['postDate' =>''], false],
                [['postDate' =>str_repeat ('a', 256)], false],
            ];
        }

    After change

    public function dataProviderNewArticle () {
            $this->createApplication ();
            $faker = \ Faker \ Factory :: create ();
            return [
                [['articleTitle' =>$faker->sentence (),
                'articleDescription' =>$faker->sentence (),
                'postDate' =>$faker->date (),
       ];
    }