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!
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).
assigns relates to the instance variables created within a controller action (and assigned to the view).
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.
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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With