paint-brush
How To Test A Controller in Ruby On Railsby@arslanbisharat
141 reads

How To Test A Controller in Ruby On Rails

by Arslan BisharatAugust 17th, 2020
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

How To Test A Controller in Ruby On Ruby On Rails is part of the first section of this article. We'll discuss a little bit philosophy of testing but more writing tests. The first test I am going to write is a controller test. The job of the index action here is to load the post using the model and then saving it as instance variable and returning some kind of response. The simplest test we can write is to test the response. In this case I have a post controller test which is generated when i generated the controller.
featured image - How To Test A Controller in Ruby On Rails
Arslan Bisharat HackerNoon profile picture

In this section, I am going to write test code. Here we"ll discuss a little bit philosophy of testing but more writing tests.

So the first test I am going to write is a controller test. the reason I am writing controller test is because a lot is going in controller as it is taking some request an responding in the response.

As you can see the job of the index action here is to load the post using the model and then saving it as instance variable and returning some kind of response.

The simplest test we can write is to test the response and which is returning right kind of response. In this case I have a post controller test which is generated when i generated the controller.

This is a simple test. Here what can I do is write get :index where get is HTTP verb and index is actually the index action and the simplest say is assert_response :success

Lets run the test:

In the console write rake

rake run our test

As you can see here test has passed