Before you go, check out these stories!

Hackernoon logoHow To Test A Controller in Ruby On Rails by@arslanbisharat

How To Test A Controller in Ruby On Rails

Author profile picture

@arslanbisharatArslan Bisharat

Full-Stack Software Developer

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


Join Hacker Noon

Create your free account to unlock your custom reading experience.