Kannan Ramamoorthy
Kannan Ramamoorthy

Reputation: 4180

Testing a laravel controller - Faking a Illuminate\Http\Request

I have a controller which is getting an instance of Illuminate\Http\Request injected through the constructor. Now I need to write an unit test that test a call in the controller which uses the values from the Request instance. I have decided to use Faker. How to use Faker to generate an associative array so that I can use the array in my test case like,

$this->post('the_uri','MyFakerArray')

And the dynamic array will automatically be available in my controllers request.

Upvotes: 2

Views: 1476

Answers (1)

Tzook Bar Noy
Tzook Bar Noy

Reputation: 11677

There is no need to fake/mock the Request object. When you are simulating a request laravel does that for you, it create a request to the url you request and pass the variables, then you get back the response from your application.

For example:

$response = $this->call('POST', '/user', ['name' => 'Taylor']);

now the $response variable has the data to test on.

I think you have:

 getContent() // for getting the reponse body
 getCode()    // for http code: 200, 401 etc

When you do that, your tests should work with the response, you have no need for Faker in this situation.

Upvotes: 4

Related Questions