In rails 4.2.0/ rspec 3.2.2/ rspec-rails 3.2.1. I'm trying to disable specs being generated when I generate new models. I'm using a spec folder structure that is different than the rails convention and would like to not have delete/move generated spec files for every new model. I tried to add generator configuration as mentioned in the rails guides and in What is the syntax to skip creating tests, assets & helpers when running `rails generate controller`?
My config/application.rb contains this:
config.generators do |g|
g.test_framework :rspec
g.model_specs false
g.view_specs false
g.helper_specs false
g.controller_specs false
g.model_spec false
g.helper_specs false
g.request_specs false
g.feature_specs false
end
and I'm still getting:
$rails g model category
invoke active_record
create db/migrate/20150416174523_create_categories.rb
create app/models/category.rb
invoke rspec
create spec/models/category_spec.rb
invoke factory_girl
create spec/factories/categories.rb
Even if I explicitly add tags:
$rails g model category --no-model-specs
invoke active_record
create db/migrate/20150416174908_create_categories.rb
create app/models/category.rb
invoke rspec
create spec/models/category_spec.rb
invoke factory_girl
create spec/factories/categories.rb
Anyone solved this before?
Simply set test_framework to something falsey if you want to disable all generators:
config.generators do |g|
g.test_framework nil
end
You sadly can't disable model specs from the generators alone. They're not optional.
Related
I'm using Rails 4
And refered these solutions by not works for me
ref
Run scaffold by
rails g scaffold Tool::Mvaas::P2pQuery --no-stylesheets user_id:integer interval:integer
But still generated scaffolds.css.scss for me
conflict app/assets/stylesheets/scaffolds.css.scss
Overwrite /Users/hsu-wei-cheng/workspace/Rails/dqa_dev_server/app/assets/stylesheets/scaffolds.css.scss?
Update
You can disable scaffold stylesheet placing this code on the file config/initializers/assets.rb:
Rails.application.config.generators do |g|
g.scaffold_stylesheet false
end
read this guides chapter "3.3 Configuring Generators".
Place into config/application.rb this code
config.generators do |g|
g.stylesheets false
end
In my Rails 3.2 app's application.rb I have the following lines to disable scaffold generators I don't want:
module MyApp
class Application < Rails::Application
# rest of the config...
config.generators do |g|
g.helper false
g.stylesheets false
g.javascripts false
end
end
end
The app is using the Draper gem and if I run rails generate then decorator is listed as one of the available generators. I assumed that adding g.decorator false to the above list would prevent rails generate scaffold SomeModel from generating the decorator files but they're still created. Can anyone tell me what I'm missing please?
Draper is configured to have decorators built by default for every controller. You can change the default configuration with one additional line in your application.rb file...
module MyApp
class Application < Rails::Application
# rest of the config...
config.generators do |g|
g.helper false
g.stylesheets false
g.javascripts false
g.decorator false
end
end
end
Here's the interesting bit from Draper...
https://github.com/drapergem/draper/blob/master/lib/generators/controller_override.rb
Called from the Railtie...
https://github.com/drapergem/draper/blob/master/lib/draper/railtie.rb
Note that you can still generate decorators explicitly...
$ rails generate decorator foo
When i run the command
rails g controller admin/inbox
Its generate test_unit and helper/test_unit. But i dont want to generate it. how to avoid it during generate controller
create app/controllers/admin/inbox_controller.rb
invoke erb
create app/views/admin/inbox
invoke test_unit
create test/functional/admin/inbox_controller_test.rb
invoke helper
create app/helpers/admin/inbox_helper.rb
invoke test_unit
create test/unit/helpers/admin/inbox_helper_test.rb
invoke assets
invoke coffee
create app/assets/javascripts/admin/inbox.js.coffee
invoke scss
create app/assets/stylesheets/admin/inbox.css.scss
You can customize your workflow by configuring config/application.rb like this..
config.generators do |g|
g.orm :active_record
g.template_engine :erb
g.test_framework :test_unit, :fixture => false
g.stylesheets false
end
for more details visit http://guides.rubyonrails.org/generators.html.
If, for some reason, you only want to skip the test generation just once, you can do this:
rails g controller admin/index --skip-test-framework
or
rails g controller admin/index --no-test-framework
This is covered in the documentation for Generators::Base.
I read the help & tried the following command to skip generation of tests, assets & helper files
$ bin/rails generate controller home index --helper false --assets false --controller-specs false --view-specs false
create- app/controllers/home_controller.rb
route get "home/index"
invoke erb
create app/views/home
create app/views/home/index.html.erb
invoke rspec
error false [not found]
error false [not found]
As you may notice by output above this works & only controller, routes & views are generated. But as last two lines are interesting:
error false [not found]
error false [not found]
Obviously rails doesn't seem to like --option-name false syntax. so this this error because I used the wrong syntax? If yes, then what is the correct way? Thanks
Try using --no- followed by optionname:
rails generate controller home index --no-helper --no-assets --no-controller-specs --no-view-specs
If you want to change the default behavior every time you run the generator command, you can configure the defaults you would like in the application.rb file - see How can I make sure Rails doesn't generate spec tests for views and helpers?.
To turn off without having to add options:
# application.rb
config.generators.assets = false
config.generators.helper = false
Applications which serve only API will not require javascript, stylesheet, views, helpers. To skip those files in generator/scaffold for Rails 3.x add the below code block in the application.rb
#to skip assets, scaffolds.css, test framework, helpers, view
config.generators do |g|
g.template_engine nil #to skip views
g.test_framework nil #to skip test framework
g.assets false
g.helper false
g.stylesheets false
end
check the link for more details about generators
More concisely:
rails g controller home index --no-assets --no-test-framework
Inside application.rb file write: This will stop generating everything apart from what is written in the command line
config.generators do |g|
g.test_framework nil
g.template_engine nil
g.assets false
g.helper false
g.stylesheets false
g.javascripts false
end
Example:
vidur#vidur-desktop:~/Downloads/tukaweb$ rails g controller uploader/three_d_models
Running via Spring preloader in process 3703
create app/controllers/uploader/three_d_models_controller.rb
invoke assets
invoke js
invoke scss
for one liner solution =>
rails g controller assets_garments --skip-test-framework --skip-assets --skip-helper
If you want to generate only controller, nothing else.
rails g controller [controller_name] [index] --no-helper --no-assets --no-template-engine --no-test-framework
Does anyone know of a rake task or RSpec call that will generate a bunch of empty files relative to the existing controllers, models, helper files and views that already exist within your application?
You can generate an empty scaffold set of rspec tests against an existing controller using something like this:
rails generate rspec:scaffold recipe
You can improve on this by passing the attributes of the model you want to generate against, like this:
rails generate rspec:scaffold recipe title: string slug: string description: text
You'll still need to do some manual editing, but this should get you most of the way there.
The best solution for this is to add hooks in place within environment.rb to create the spec.rb files within the rails application each time a model or controller is created.
Here's the code for that (using RSpec and FactoryGirl):
module RailsApp
class Application < Rails::Application
config.generators do |g|
g.test_framework :rspec, :fixture_replacement => :factory_girl, :views => true, :helper => false
g.fixture_replacement :factory_girl, :dir => 'spec/factories'
g.stylesheets false
g.javascripts false
g.helper false
end
end
end
This should work:
Install the rspec-rails gem by adding it to your development and test groups in your gemfile gem 'rspec-rails'
Run the rspec generator from inside your app rails generate rspec:install
Read over this doc quickly to see how it integrates with your rails app RSpec-rails doc