Unit Test Laravel's FormRequest

前端 未结 2 1816
北恋
北恋 2020-12-02 23:34

I am trying to unit test various custom FormRequest inputs. I found solutions that:

  1. Suggest using the $this->call(…) method an

2条回答
  •  庸人自扰
    2020-12-03 00:20

    Friends, please, make the unit-test properly, after all, it is not only rules you are testing here, the validationData and withValidator functions may be there too.

    This is how it should be done:

    setContainer(app())
                    ->setRedirector(app(Redirector::class))
                    ->validateResolved();
            } catch (ValidationException $ex) {
    
            }
            //\Log::debug(print_r($ex->errors(), true));
    
            $this->assertTrue(isset($ex));
            $this->assertTrue(array_key_exists('the_address', $ex->errors()));
            $this->assertTrue(array_key_exists('the_address.billing', $ex->errors()));
        }
    
    
        public function test_AddressesRequest_success_billing_only()
        {
            $faker = FakerFactory::create();
            $param = [
                'the_address' => [
                    'billing' => [
                        'zip'        => $faker->postcode,
                        'phone'      => $faker->phoneNumber,
                        'country_id' => $faker->numberBetween(1, Country::count()),
                        'state'      => $faker->state,
                        'state_code' => str_random(2),
                        'city'       => $faker->city,
                        'address'    => $faker->buildingNumber . ' ' . $faker->streetName,
                        'suite'      => $faker->secondaryAddress,
                    ]
                ]
            ];
            try {
                //app(AddressesRequest::class);
                $request = new AddressesRequest($param);
                $request
                    ->setContainer(app())
                    ->setRedirector(app(Redirector::class))
                    ->validateResolved();
            } catch (ValidationException $ex) {
    
            }
    
            $this->assertFalse(isset($ex));
        }
    
    
    }
    

提交回复
热议问题