Reputation: 8986
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?
Upvotes: 4
Views: 3278
Reputation: 3167
As a footnote, if your generator command does not generate a file you expect, check the application.rb
config file to make sure specific specs were not disabled.
For me following generator did not create file as expected.
% bin/rails g rspec:request api
Running via Spring preloader in process 70924
%
because in application.rb
g.request_specs false
was set. While this seems obvious after the fact, command output is not intuitive. Setting it to true
and re-running the generator produces expected result.
% bin/rails g rspec:request api
Running via Spring preloader in process 71843
create spec/requests/apis_spec.rb
Upvotes: 0
Reputation: 11647
Rspec doesn't automatically create specs for your existing models and controllers. You'll have to go create those files yourself now.
Creating a spec file is really easy. Just make a file ending in _spec.rb and put this in it:
require 'spec_helper';
describe User do;
end
(of course replacing User with the class you are testing) and you're there.
Upvotes: 3
Reputation: 5529
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.
Upvotes: 8