RSpec generates request specification instead of controller specification

I use rspec-rails, but I would like to change when rails g controller Blahs , I would like to create spec / request / instead of spec / controllers /.

Thanks!

+4
source share
3 answers
 rails g integration_test foo 

And you are fine!

+11
source

There seems to be no ordinary way to achieve this, however I found a solution to fix the monkeys:

 # config/initializers/generators.rb Rails.application.configure do config.generators do |g| # your setup here, like g.javascripts = false end def self.load_generators(*) super require 'rails/generators/rails/controller/controller_generator' Rails::Generators::ControllerGenerator.class_eval do remove_hook_for :test_framework hook_for :test_framework, as: :request end end end 

Here are some explanations: hook for test framework is installed directly inside the ControllerGenerator, so we need to load the class to override the value. I do not want to load this class anyway, except for running generators. config.generators a block is executed for the console and server, so it is not suitable. There is also a self.generators block, but it runs until config.generators , and the generator will not be configured. So I found the Engine#load_generators method that works for me.

Tested with rails 5.0.1.

+5
source

I don't think you can configure this, but you can always use fork rspec-rails and configure it as you want https://github.com/rspec/rspec-rails/blob/master/lib/generators/rspec/controller/ controller_generator.rb

Good luck

0
source

Source: https://habr.com/ru/post/1414445/


All Articles