RSpec added midway - how to generate stubs of all existing resource - ruby-on-rails

I added RSpec mid way and want to have RSpec create all the stub test cases for the existing resources. Any quick way to do so?

rails generate <type> <name> -s
If its a scaffold, you need to temporarily remove the migration so as to fool the generator, and then answer "no".

You could try:
rails generate scaffold <model_name>
and answer "no" when asked to overwrite existing files. It might be less stressful emotionally to do this in a new project and then copy over the generated spec files.

Related

How to test generators with RSpec

Is there a recommended way of testing Rails generators with RSpec?
The only solution I've found is the Generator Spec gem, that hasn't been updated in over two years.
There's the gem
https://github.com/petergoldstein/generator_spec, which does a decent job, although it is not very actively maintained
I would write a file by hand that acts as a test fixture. I would then as part of my test generate the file with the generator. At that point I would diff the two files. Looks like the diffy gem could help you there. If there is no diff then pass the test. Fail if otherwise.
https://github.com/samg/diffy
Don't forget to clean up your temp files after the tests. You don't want them hanging around in your repos.
I didn't found official recommendations how to test Rails generators too. So, I am just run generator directly on dummy application, compare generated files and delete them at the end of test.
Here is my spec which implement described approach. I had used minispec instead of rspec here but they a very similar.

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).

Rails: renaming a controller and corresponding model

Is there an easy way to rename a controller and model in my app and all the instances in the corresponding code?
I'm using textmate, would this be as simple as using the replace function and replacing the word Post with Report?
You need to change the name of the Controller and the associated Model, Views, Helpers, Tests & Routes directories, file names, class names & the names in the class definitions.
I found two ways to do this but before you try anything I recommend you back-up your app, preferably with a Software Version Control system like Git & Github.com.
Your first option is to do it manually & there is a good explanation on how to do this here: How to rename rails controller and model in a project
Another way is to destroy your controller & model, and then generate a new one, this will remove all the files which were generated the first time round & replace them with new ones. Michael Hartl explains this solution well in his online guide to Ruby on Rails here: http://ruby.railstutorial.org/chapters/static-pages#sidebar-undoing_things
This is the solution I followed when I needed to make this change to my app, I needed to replace a MVC scaffold I generated called board with a new one called product.
1. First
I made a back-up of the work I did in the layout of the board view, app/views/boards/index.html.erb
2. Then
I ran the below rails commands in the terminal window.
$ rake db:rollback
$ rails destroy scaffold board name:string description:text image:string price:decimal
$ rails generate scaffold product product_type:string name:string description:text image:string price:decimal
$ rake db:migrate
3. Finally
I copied my backed-up boards/index.html.erb file into the newly generated app/views/products/index.html.erb & did a find & replace in my text editor on this file to replace board with product.
I think the second option is much more reliable & quicker but it’s important to make this change early on in your project before you make too many manual changes to the code. It would be better to just take a little more time planning your MVC names & database tables properly before you start your project.
You can also use rails_refactor gem to rename controller, model, etc
for more info check https://github.com/jcrisp/rails_refactor
To rename controller and model use this gem https://github.com/jcrisp/rails_refactor
If you are using textmate, use 'command-shift-f" to look for a string throughout your entire project.
Yes and no. You can rename it that way, but you'll also need to rename the files as well or Rails won't know where to look for the files corresponding to the new Report model/controller/etc.

Generate only tests from existing model / controllers

I have a Rails3 app that is based on someone else's work. For some reason they decided not to supply the tests with the app, which I am finding frustrating.
What I want to be able to do is scaffold the tests for all the existing controllers and models so I can get a head start on creating the tests myself in test::unit. I don't want to recreate the models or controllers, just create the tests.
I am new to Rails, and have hunted about for the rake command that might do this, but all with no luck so far. Any advice / direction most appreciated.
I know it's a little old, but you can do this:
rails g scaffold Post -s
The -s makes it skip the files already created. Also, if you don't use the flag it just asks you if you want to override the file, so, no worries.
To only generate the associated test files for an existing Rails 3 app, I use "generate resource" but skip everything that I don't want:
rails g resource Post --skip --no-resource-route --no-migration --no-helper --no-assets
Other options can be found using rails generate resource --help
-s, [--skip] # Skip files that already exist
--resource-route # Indicates when to generate resource route
[--helper] # Indicates when to generate helper
[--assets] # Indicates when to generate assets
[--migration] # Indicates when to generate migration
Why not use generate scaffold? Because it might generate views that I'm not using.
There's no way to do this that I'm aware of. It would be pretty easy though to just create a temporary rails project and generate scaffolds for all of your models then copy the resulting test directory into the real project.
I.e.
rails new temporary
cd temporary
rails g scaffold Post title:string body:text
rails g scaffold Comment post:references author:string body:text
cp -r test ../real_rails_app/
etc.
This answer is now out of date. Up to date rails versions allow you to generate only the missing files with the skip option.

Adding a hook to script/generate migration

I do my Rails dev from xterm and in vim.
I'm getting sick of running script/generate migration do_whatever, then trawling through db/migrate trying to tab-complete to 20091015235018_do_whatever when there's 5 other migrations with similar timestamps.
What's the best way to add a hook to open the generated migration in vim?
I'd rather not hack into Rails' core in /usr/lib as I work from several systems and can see myself wanting to create numerous such hooks. However, it doesn't really seem plugin worthy.
There are lots of simple ways to get the name of the generated file, but I'm not sure how to cleanly hook it into the generation. What do you think?
A better option with vim is to use rails.vim and type :Rmigration do<TAB>
It ignores the timestamp ...
You can also create and edit a migration in one go by typing :Rgenerate migration ...
I just submitted a patch to Rails to add an --editor option for just this case, which you could backport into whatever version of Rails you're using now. If you don't specify which editor you want it tries to use EDITOR

Resources