From my situation, I need to test my request validation rules, but here is a problem, I think I DO NOT make a request to the real route, so I have to create a fake controller and add to my test case, here is my solution.
1/**2 * @dataProvider resend3 */4public function testOrderResendRequest($payload, $assertions)5{6 $route = Route::addRoute('POST', 'fake/resend', '\Tests\Feature\FakeController@resend')7 ->prefix('api')->name('fake.resend');8 $this->json('POST', $route->uri, $payload)9 ->assertJsonValidationErrors($assertions);10}
I added a route to request, and also making a fake controller in the test case, finally, I did a json request to the fake route. By the way, I used @dataProvider to validate any situations I wanted to test properties, it’s really useful.
Next, I made FakeController
in the same class.
1class FakeController extends Controller2{3 public function resend(OrderResendRequest $request) // Here is my request layer.4 {5 return response()->json(['data' => 'hello']); // whatever you wanted putting in.6 }7}89public function testOrderResendRequest($payload, $assertions)
What do I need to do this? I used validator()->make()
, I can’t validate Rule, for example, Rule:requireIf and others closures, if your rules are only having string rules, you can do for:
1$rules = (new OrderResendRequest)->rules();2$validator = validator()->make($payload, $rules);34$this->assertFalse($validator->fails());5// and what you want to test