19

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.

kelso
  • 394
  • 1
  • 2
  • 11

3 Answers3

24

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.

Koen.
  • 25,449
  • 7
  • 83
  • 78
Zequez
  • 3,399
  • 2
  • 31
  • 42
20

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.

sina
  • 1,091
  • 1
  • 8
  • 6
0

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.

Dave Rapin
  • 1,471
  • 14
  • 18
  • Agree, was in the same situation, tried all advices I was able to google and only this one really helped –  Sep 30 '14 at 12:58