Generate all RSpec specification files from existing controllers, models, and views in a Rails application

Does anyone know of a rake job or an RSpec call that will generate empty empty files compared to existing controllers, models, auxiliary files and views that already exist in your application?

+8
ruby-on-rails tdd testing rspec
source share
3 answers

The best solution for this is to add hooks within environment.rb to create spec.rb files in the rails application every time you create a model or controller.

Here is the code for this (using RSpec and FactoryGirl):

 module RailsApp class Application < Rails::Application config.generators do |g| g.test_framework :rspec, :fixture_replacement => :factory_girl, :views => true, :helper => false g.fixture_replacement :factory_girl, :dir => 'spec/factories' g.stylesheets false g.javascripts false g.helper false end end end 
-one
source share

You can create an empty rspec test sketch set for an existing controller using something like this:

 rails generate rspec:scaffold recipe 

You can improve this by passing in the attributes of the model you want to create, for example:

 rails generate rspec:scaffold recipe title: string slug: string description: text 

You still need to do manual editing, but that should do you most of the way.

+2
source share

This should work:

  • Install rspec-rails gem by adding it to your development and testing team in your gemfile gem 'rspec-rails'
  • Run the rspec generator from your rails generate rspec:install application
  • Read this document quickly to find out how it integrates with your rails RSpec-rails doc device.
-4
source share

All Articles