How do I generate a controller spec using rspec?

spinlock picture spinlock · Jun 7, 2011 · Viewed 17.6k times · Source

I'm integrating devise_invitable into my application and I had to write a custom controller - InvitationsController - to override a few methods in the gem. Now, I want to write tests to cover what I've done but I can't figure out how to generate a spec for the new controller. Any help would be greatly appreciated.

Thanks!

Answer

prasad.surase picture prasad.surase · Jun 25, 2013

the rspec way is

rails g rspec:controller passwords

it gives

create  spec/controllers/passwords_controller_spec.rb

--Update

You can configure your application to generate rspec when model or controller is created. Add to config/application.rb

# don't generate RSpec tests for views and helpers.
config.generators do |g|
  g.test_framework :rspec, fixture: true
  g.fixture_replacement :factory_girl, dir: 'spec/factories' 

  g.view_specs false
  g.helper_specs false
end

$rails g model category
  invoke  mongoid
  create    app/models/category.rb
  invoke    rspec
  create      spec/models/category_spec.rb
  invoke      factory_girl
  create        spec/factories/categories.rb
$rails g controller categories
  create  app/controllers/categories_controller.rb
  invoke  haml
  create    app/views/categories
  invoke  rspec
  create    spec/controllers/categories_controller_spec.rb
  invoke  helper
  create    app/helpers/categories_helper.rb
  invoke    rspec
  invoke  assets
  invoke    coffee
  create      app/assets/javascripts/categories.js.coffee
  invoke    scss
  create      app/assets/stylesheets/categories.css.scss