3

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?

4

3 に答える 3

8

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.

于 2017-09-15T17:46:44.397 に答える
3

Rspecは、既存のモデルとコントローラーの仕様を自動的に作成しません。今すぐ自分でこれらのファイルを作成する必要があります。

スペックファイルの作成はとても簡単です。_spec.rbで終わるファイルを作成し、その中に入れます。

require 'spec_helper';
describe User do; 
end 

(もちろん、Userをテストしているクラスに置き換えます)そしてあなたはそこにいます。

于 2012-05-04T18:41:45.463 に答える
0

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
于 2021-07-14T10:33:06.133 に答える