J-orge Mata Villalobos
J-orge Mata Villalobos

Reputation: 109

How to generate automatic Rspec tests for existing scaffold

I have an application on Ruby on Rails. All files have been modified from what the scaffold by itself generates. I want to include automatic testing with the Rspec gem, but I want it to generate all the automatic tests it normally does (since this is my first time working with rspec).

I have seen that the usual way to do it is to install Rspec before generating the scaffolds (and when you generate the scaffold the tests are automatically generated), but since everything is already generated, how can I generate the tests only?

I have got all the way to the rails generate rspec:install command, getting it ready for the test runs.

How can I automatically generate the tests for an existing scaffold?

Upvotes: 5

Views: 4670

Answers (3)

Luke Loera
Luke Loera

Reputation: 21

Specifically for Rails 6, you can add the --skip-collision-check and --skip flags to ignore the files that already exist.

The first flag permits rails to proceed with a scaffold if there is a collision. The second flag tells rails what to do when it encounters a collision. In this case, it skips the original (i.e. pass over or leave the original file alone). So after you've installed RSpec, run your original scaffold again...

rails g scaffold Example title:string --skip-collision-check --skip

and then Rails will generate whatever RSpec files you're missing (requests, routing, views, etc.)

Upvotes: 0

ivan-uqido
ivan-uqido

Reputation: 61

rails g rspec:scaffold scaffold_name

where scaffold_name is the name you used in the scaffold generation, in this case rails g scaffold scaffold_name

Upvotes: 6

zetetic
zetetic

Reputation: 47578

The generators will prompt before overwriting any existing files. Just add RSpec and re-run the generator(s), skipping any files you want to keep.

If you are paranoid (and why shouldn't you be?), commit your work first.

Upvotes: 4

Related Questions