List of RSpec's generators - ruby-on-rails

I'm looking for a comprehensive list of RSpec's generators to easily generate specs for controllers, models, helpers, and so on. The only one I've found is:
rails g integration_test name
that saves a spec inside the spec/requests folder.

controller
helper
install
integration
mailer
model
observer
scaffold
view
example usage:
rails g rspec:integration events
--> create spec/requests/events_spec.rb

All the rspec-rails generators can be found at https://github.com/rspec/rspec-rails/tree/master/lib/generators/rspec You'll have to dig around in the code a little to see what they do, but they are well organized so it shouldn't be too much of a pain.
There's also a short readme on the generators which basically says that they are run automatically when you run one of the standard Rails generators (rails g model User):
If you type script/rails generate, the only RSpec generator you'll
actually see is rspec:install. That's because RSpec is registered with
Rails as the test framework, so whenever you generate application
components like models, controllers, etc, RSpec specs are generated
instead of Test::Unit tests.

Related

Rails hooking a custom generator into scaffold generator

In a new Rails 6.1.3 project, I'm attempting to have bin/rails generate scaffold Thing name:string invoke the typical Rails scaffolding (including active_record and scaffold_controller) as well as invoking my own custom generator to create an additional ThingForm that I want to be a part of my scaffolding.
I created a custom generator for form that works just fine by itself. bin/rails generate form Thing name:string is properly setting up an app/forms/thing_form.rb.
But in attempting to get this generator hooked into the default scaffolding generator, I'm running into different problems with different approaches I've tried.
Inspired by active_model_serializers, when I set up a hook_for :form as part of Rails::Generators::ResourceGenerator it ends up no longer invoking active_record. Here's the commit from my example repo: https://github.com/diachini/generator_hook_investigation/commit/8989b8f96b05ac1157d1d3a247bee9f0f2befe5d
To confirm I wasn't crazy, I have another branch that just confirmed generating a scaffold with active_model_serializers installed properly keeps the active_record invocation happening.
Taking a page out of this Gist explaining custom generators - I similarly attempted a hook_foron the ScaffoldGenerator and the ControllerGenerator
Just like my active_model_serializers attempt, this no longer invoked active_record.
It also began ignoring config for turning off assets and scaffold_stylesheet.
On a separate branch in my example repo: https://github.com/diachini/generator_hook_investigation/commit/86bc7b53ac34ea29d5e32d07486c73d962f86247
I saw a similar StackOverflow question with an update regarding copying the whole ScaffoldGenerator, but was hoping to avoid that approach if there's something simple that I'm missing with the hook_for approach that active_model_serializers took.

Rails Tutorial (M. Hartl) Chapter 3, Missing `static_pages_controller_test.rb` folder after `rails generate`?

I'm working on Michael Hartl's RoR Tutorial Chapter 3.2.1 and I'm generating a Static Pages controller using the command:
$ rails generate controller StaticPages home help
But after doing this I cannot see the see the test/controllers/static_pages_controller_test.rb file being generated like in the book (see tenth line):
$ rails generate controller StaticPages home help
create app/controllers/static_pages_controller.rb
route get 'static_pages/help'
route get 'static_pages/home'
invoke erb
create app/views/static_pages
create app/views/static_pages/home.html.erb
create app/views/static_pages/help.html.erb
invoke test_unit
create test/controllers/static_pages_controller_test.rb
invoke helper
create app/helpers/static_pages_helper.rb
invoke test_unit
create test/helpers/static_pages_helper_test.rb
invoke assets
invoke coffee
create app/assets/javascripts/static_pages.js.coffee
invoke scss
create app/assets/stylesheets/static_pages.css.scss
Here's what it looks like when I run it:
My folder is also completely empty and cannot run any tests.
I think you may have mixed up editions of Michael Hartl's Ruby On Rails tutorial.
The 3rd edition uses Minitest instead of RSpec. However you app seems to be setup to use RSpec which is a different test framework which was used in the 2nd edition.
If you want to know why its generating RSpec specs instead of tests then its most likely because you ran:
rails generate rspec:install
Which changes the generators to generate specs instead.
The simplest way to remedy this is to delete your current sample_app directory and start over from the beginning of chapter 3 and make sure you only use the current edition:
https://www.railstutorial.org/book/static_pages
There are other ways but it might be easier to avoid other mishaps down the line if you get a fresh start.
Because you use rspec for testing and the generator command creates files and folders related to rspec suite. Instead of creating:
test/controllers/static_pages_controller_test.rb
it creates:
spec/controllers/static_pages_controller_spec.rb
so if you want to follow Michael Hartl's RoR Tutorial you must change your testing method to minitest.

regenerating rspec files

how can I generate rspec on-demand?
the thing was my rspec files were already automatically generated by the "rails generate controller" command. Then I manually deleted those files in hope that there should be a command which I can use to regenerate those files.
What do I do to regenerate those deleted rspec files without firing "rails generate controller" again?
I have tried some command I was suggested by some blog:
$ rails generate rspec_controller pages --skip-migration --skip-fixture --skip
Could not find generator rspec_controller.
but it didn't work for me.
any advice would be really appreciated!
In the root folder of your app:
rails generate controller -h
It will show you the usage instructions.
rspec_controller is deprecated in favour of the standard controller generator in Rails. It works now by you including rspec-rails within your Gemfile like this:
group :development, :test do
gem 'rspec-rails'
end
This then will load this file, which customizes what tools the Rails controller generator uses with these two lines.
This is a bad idea, for two reasons:
You normally shouldn't be autogenerating your spec files: you need to think about exactly what you want to specify.
Controller specs should normally not be written at all. Use Cucumber scenarios instead -- they're much less painful and much better at testing behavior (controller specs are too dependent on implementation).

How to add first Cucumber test to a Rails app

Confession: I have never written a single test for Rails.
I have installed the gems cucumber, rspec, capybara, factory girl. Running Rails 3.1.
I am not sure, um, where to create a new test file or what to name it.
Thanks for your patience.
Micheal Hartl has a good tutorial on Rails that is mostly test driven:
http://ruby.railstutorial.org/
You probably know most of this but it will point you in the right direction.
Here's a Rails Cast on Cucumber:
http://railscasts.com/episodes/155-beginning-with-cucumber
Here's an RSpec Rails Cast:
http://railscasts.com/episodes/71-testing-controllers-with-rspec
Here are a bunch of Cucumber examples:
https://github.com/cucumber/cucumber/tree/master/examples/i18n
Hope that helps!
after installing the rspec and cucumber
you must run following commands
rails generate rspec:install for rspec
first command
will configure rails generate command and it will create the spec directory which will contain tests for your models, controllers, views in respective directory you can write the
rspec test
eg. If you are having user model then specs for user will go in
spec/models/user_spec.rb
that's it
to run these tests use
rspec spec/models/user_spec.rb
which will output the whether the tests are passed or not
cucumber describes the behavior of application
and rspec describes behavior of object
rails generate cucumber:install for cucumber
which will create features directory in your application root
inside that you can write cucumber test with .feature extension
eg. If your application have feature like creating user, this feature will go in
features/creating_user.feature file
and the step definition for this feature will go in
features/step_definitions/create_user_steps.rb
well its just short guide line you can refer the following links
for cucumber
http://loudcoding.com/posts/quick-tutorial-starting-with-cucumber-and-capybara-bdd-on-rails-project/
Think what is the most common way for people to use your app. Write a test for the 'happy path', ignoring any edge cases.
Next, write tests for the parts most likely break.

RSpec/Gem development: Stubbing rails templates

I'm currently working on a couple of different gems both of which mainly consist of rails view helpers.
My problem is that some of these helpers require rendered rails templates to test the output - but I am unsure of the best way to stub a template, without a rails application. I presume that I should be able to leverage rspec-rails in some capacity, but i've been having trouble getting that to work without a rails app present.
Am I approaching this the wrong way? What's the current best-practice to test rails-specific features (in particular - things that happen only in the view) during gem development?
I use the excellent enginex gem, which helps you in setting up a gem skeleton with an embedded rails application for testing. This way you can easily test any rails dependency inside that application. To generate rspec tests run it as follows (default is test-unit):
enginex -t rspec your-gem-name
What I did to incorporate this into my gem, was run this inside some test folder, and copied the necessary files over to my gem. As an example, you could check it out inside my cocoon gem.
Hope this helps.

Resources