Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I generate a controller spec using rspec?

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!

like image 916
spinlock Avatar asked Jun 07 '11 21:06

spinlock


People also ask

What are controller specs?

Controller specs are RSpec wrappers for Rails functional tests. They simulate a single HTTP request in each example. By default views are not rendered; the controller spec stubs views with a template that renders an empty string (the template must exist).

What is assigns RSpec?

assigns relates to the instance variables created within a controller action (and assigned to the view).

How do I set up RSpec?

Installing RSpecBoot up your terminal and punch in gem install rspec to install RSpec. Once that's done, you can verify your version of RSpec with rspec --version , which will output the current version of each of the packaged gems. Take a minute also to hit rspec --help and look through the various options available.

How do I run a RSpec test on a file?

Running tests by their file or directory names is the most familiar way to run tests with RSpec. RSpec can take a file name or directory name and run the file or the contents of the directory. So you can do: rspec spec/jobs to run the tests found in the jobs directory.


2 Answers

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 
like image 127
prasad.surase Avatar answered Oct 02 '22 17:10

prasad.surase


If you are using Rails 3 + rspec and you installed rspec (rails g rspec:install), it should generate controller specs for each controller you generate (and others objects).

If you need to create one by hand. Just create a new new_controller_name_spec.rb in your spec/controllers.

require 'rails_helper'  describe NewControllerName do   # Test! end 

You could also try to re-generate the controller file, say No when it asks you if you want to overwrite the existing controller, and hopefully it will regenerate the rspec for that controller again.

like image 34
nowk Avatar answered Oct 02 '22 16:10

nowk