Spec directory in Rails

Are you curious about what the spec directory contains in your Rails project? I’d like to write briefly about these directories.

RSpec generates /spec directory with many subdirectories when you call rails generate rspec:install in your rails application. You can extend it with special ones, but the defaults are the following:


    /fixtures: Contains attachments, database schemas for the specs.
    /routings: Routing specs test your /config/router.rb, which means different requests are directed to the proper controller having valid parameters. In some cases we place some parameters directly to the URL and make it parsed by /:date/:id like matchers. In this case you can check whether your matcher works properly or not.
    /controllers: Controller specs test your actions work properly. These tests responsible for actions do the right process for any possible input, and provide information outside in every cases. We don’t check the rendered view content here, just the response value and instance variables that was set. You can use database fixtures to fill up the database tables with test data and create environment for the action.
    /models: Model specs test the consistency and the relation interface of a model (has_many, belongs_to). In consistency check we use pre- and post validators, that means being in a pre-state and calling a method on the model, it goes to the post-state. We check the model’s getters, setters and validators here.
    /views: View specs test whether the rendered views contain the information provided by the instance variables of the controller. Here we prepare the instance variables and call render method afterwards. The result is a html structure that can be processed by different xml processors to validate it. These specs must be independent from controllers, you mustn’t call any action before testing, you can manipulate the data by setting mocks for the instance variables.
    /mailers: Mailer specs test the header fields of a mail and check the template contains all the necessary information. You can use it for testing confirmation mails.
    /helpers: Helper specs test your views’ helpers. There are really simple tests, just check simple helper methods.
    /request: Request specs test your application under black box. These tests call URLs, fill forms, click to buttons, check results. These tests act like a user who is browsing your app.

You can find more information on RSpec page about it!