InaniELHoussain's avatar

How to Test Controller with FormRequest

As the Title says I'm trying to look for a way to test a method on controller as simple as that

public function (MyCustomRequest $request) { $this->postRepository->create($request->all()); }

I'v looked every where but I've not found any way .

0 likes
6 replies
michaeldyrynda's avatar

If you use Laravel's built in integration testing, you can simply submit the form that posts to that controller method. That way you can check any redirects / success messages, too.

If you only want to test the repository method itself, you can write tests for the repository and just assert values exist in your persistence layer.

1 like
InaniELHoussain's avatar

for example I'm trying to test that method on the controller

public function store(MyCustomRequest $request) { $this->postRepository->create($request->all()); }

I want to test it without passing the form, Just Mocking the Request

michaeldyrynda's avatar

In that case, wouldn't you just want to test the repository method itself? Pass an array of values to the create() method and make your assertions against that.

1 like

Please or to participate in this conversation.