Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When writing RSpec tests with rails, what should go in the spec/requests folder vs spec/controllers?

I'm fairly new to rails and trying to do things the "right" way by implementing tests from the get go. Yesterday I used the scaffolding generator to create my first model/view/controller configuration. While I've been told that you really shouldn't be using scaffolding, it was useful so that I could learn how Rails code is structured.

The one thing I noticed was that the automatically generated RSpec was mostly placed in the spec/controllers folder. However when I watched this episode of Railscasts, I noticed that he used the

rails generate integration_test [test_name]

command which placed a single test file in the spec/requests folder. However all of his tests that he wrote interacted with the controllers. What I'm trying to determine is the best practice for where to store these tests.

When should one store tests in the spec/requests folder and when should one store tests in the spec/controllers folder? Any feedback would be greatly appreciated!

like image 833
Nick ONeill Avatar asked Aug 03 '12 18:08

Nick ONeill


1 Answers

Actually those are 2 types of tests. In the controller folder you should create tests to test the controller actions, in the request folder you should place tests to interact with views, wich will actually test all your application parts, and that's why it's named integration test.

Here are some articles about those two types of tests.

http://everydayrails.com/2012/04/07/testing-series-rspec-controllers.html

http://everydayrails.com/2012/04/24/testing-series-rspec-requests.html

like image 79
Ismael Avatar answered Nov 03 '22 00:11

Ismael