Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RSpec isn't generating spec files for my models and controllers

I have a Rails application using Rails 3.

I added rspec-rails to my Gemfile:

group :development, :test do
  gem 'rspec-rails'
end

and then I run bundle install. It shows my gem list, and all rspec gems are there (core, rails, etc.).

However, when I run

rails g rspec:install

that's what it returns:

create  .rspec
create  spec
create  spec/spec_helper.rb

Although I have models and controllers in my application, it just create those files. Why isn't Rspec creating the spec files?

like image 945
João Daniel Avatar asked Oct 28 '25 02:10

João Daniel


1 Answers

This is no longer true, and you can set the generator to create RSpec spec files when generating new parts of your rails application, as well as creating spec files for existing sections of the app.

The main feature lies in the application's generator configuration which is enabled when running the rspec-rails rspec:install task, but if you want to specify specific spec files to include/exclude, you may want this:

config/environments/development.rb // or any env you want

Rails.application.configure do
...
  config.generators do |g|
    g.test_framework :rspec
    g.fixture_replacement :factory_bot
    g.factory_bot dir: 'spec/factories'
    g.controller_specs false
    g.request_specs true
    g.helper_specs false
    g.feature_specs true
    g.mailer_specs true
    g.model_specs true
    g.observer_specs false
    g.routing_specs false
    g.view_specs false
  end
end

Generator Settings

The 'test_framework' option allows the rails to know exactly what test framework to create test files for, and generate new files based on your settings.

With the 'fixture_replacement', we can keep rails from generating fixtures by default, and instead create factories with each model created.

Lastly are the 'factory_bot' options, where you can change the factory folder default if needed, but will default to this directory on install. You can find more options in the Factory Girl/Bot Instructions.

Now when we generate something new, like a model:

> rails g model settings
  invoke  active_record
  create    db/migrate/20170915173537_create_settings.rb
  create    app/models/setting.rb
  invoke    rspec
  create      spec/models/setting_spec.rb
  invoke      factory_girl
  create        spec/factories/settings.rb

Generating Spec Files For Pre-Generated Sections of the App

Similar to generating rails files, you can generate spec files through Rspec's own task:

> rails g rspec:model old_settings
  create  spec/models/old_settings_spec.rb
  invoke  factory_girl
  create    spec/factories/old_settings.rb

This command uses the same conventions as the rails' generate command to create spec files, including scaffold, so you can create spec files for an entire namespace.

like image 113
DBrown Avatar answered Oct 29 '25 23:10

DBrown



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!