Generate only tests from existing model / controllers

前端 未结 3 628
借酒劲吻你
借酒劲吻你 2020-12-31 02:26

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

相关标签:
3条回答
  • 2020-12-31 03:08

    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.

    0 讨论(0)
  • 2020-12-31 03:22

    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.

    0 讨论(0)
  • 2020-12-31 03:24

    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.

    0 讨论(0)
提交回复
热议问题