0
0

More than 3 years have passed since last update.

RailsにRspecを導入してみた

Last updated at Posted at 2020-04-14

新しく開発をしようと考え、テストの方も勉強したいなと感じたのでメモがてらRailsにRspecを導入する方法まとめてみました。

1 gemのインストール

Gemfile
group :test do
  gem 'rspec-rails'
  gem 'factory_bot_rails'
  gem 'rails-controller-testing'
end

Gemfileに上記のgemを記述します。

2 bundle install

$ bundle install

3 Rspecに必要なファイルを作成

bundle exec rails generate rspec:install

すると以下のファイルが作成されます。

.rspec      
 spec
 spec/spec_helper.rb
 spec/rails_helper.rb

4 Factory_botの設定

spec/rails_helper.rbの最下部に以下の記述をします。

spec/rails_helper.rb
RSpec.configure do |config|
  config.include FactoryBot::Syntax::Methods
end

5 rails g コマンドと同時にテストファイルを作成させる

application.rb
config.generators do |g|
      g.test_framework :rspec,
                       fixtures: true,
                       view_specs: false,
                       helper_specs: false,
                       routing_specs: false,
                       controller_specs: true,
                       request_specs: false
      g.fixture_replacement :factory_bot, dir: "spec/factories"
    end

6 Rspecが導入されているかの確認

$ bundle exec rspec
No examples found.

Finished in 0.00065 seconds (files took 0.10275 seconds to load)
0 examples, 0 failures

と表示されれば導入は完了です!

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0