简体   繁体   English

仅生成现有模型/控制器的测试

[英]Generate only tests from existing model / controllers

I have a Rails3 app that is based on someone else's work. 我有一个基于别人工作的Rails3应用程序。 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. 我希望能够做的是为所有现有的控制器和模型构建测试,这样我就可以在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. 我是Rails的新手,并且已经找到了可能会执行此操作的rake命令,但到目前为止一切都没有运气。 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. -s使它跳过已创建的文件。 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 3应用程序生成关联的测试文件,我使用“生成资源”但跳过我不想要的所有内容:

rails g resource Post --skip --no-resource-route --no-migration --no-helper --no-assets

Other options can be found using rails generate resource --help 使用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 ? 为什么不使用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. 只需创建一个临时rails项目并为所有模型生成脚手架然后将生成的测试目录复制到实际项目中将非常容易。

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. 最新的rails版本允许您使用skip选项仅生成缺少的文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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