简体   繁体   中英

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.

Ie

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM