LoginSignup
1
0

More than 1 year has passed since last update.

【RSpec】RSpecの導入

Posted at

はじめに

Railsなどを中心に勉強中のエンジニア初心者が他の記事を参考にしたり、実際に実装してみたりして、アウトプットの一環としてまとめたものです。
間違っていることもあると思われるので、その際は指摘いただけると幸いです。

RSpecのGitHub

rspecのインストール

Gemfileへの記載

Gemfileに下記の通り追記する。

Railsのバージョンに合わせてどのバージョンのrspecをインストールするかは、GitHub上で確認することができる。

テストデータを作成するためのFactory_BotというGemも、テストを実行する際によく使用するため一緒にインストールすることが多い。

group :development, :test do
  gem 'rspec-rails', '~> 4.0.0' #Rails 5.xでRspecを使用する場合、このようにバージョン指定する。
  gem 'factory_bot_rails'
end

Gemのインストール

bundle installを実行してGemをインストールする。

bundle install

rspecの設定ファイルを作成する

下記のコマンドを実行することで、rspecの設定ファイルが生成される。

実行コマンド

bundle exec rails g rspec:install

実行結果

$ bundle exec rails g rspec:install
DEPRECATION WARNING: Spree::Order state machine defined in Spree::Order::Checkout is deprecated. Future versions of Solidus will use Spree::Core::StateMachines::Order} (called from <main> at /testec/config/environment.rb:5)
DEPRECATION WARNING: Spree::Config.raise_with_invalid_currency set to true is deprecated. Please note that by switching this value, Spree::LineItem::CurrencyMismatch will not be raised anymore. (called from <main> at /testec/config/environment.rb:5)
DEPRECATION WARNING: Spree::Config.consider_actionless_promotion_active set to true is deprecated. Please note that by switching this value, promotions with no actions will be considered active. (called from <main> at /testec/config/environment.rb:5)
DEPRECATION WARNING: Spree::Config.run_order_validations_on_order_updater set to false is deprecated and will not be possibile in Solidus 3.0. Please switch this value to true and check that everything works as expected. (called from <main> at /testec/config/environment.rb:5)
DEPRECATION WARNING: Spree::Config.use_legacy_address_state_validator set to true has been deprecated and will be removed in Solidus 3.0. The Spree::Address state validation has been extracted into a configurable external class. Switch Spree::Config.use_legacy_address_state_validator to false to start using the external validation class. (called from <main> at /testec/config/environment.rb:5)
      create  .rspec
      create  spec
      create  spec/spec_helper.rb
      create  spec/rails_helper.rb

生成されるファイル

  • .rspec
    • 基本設定ファイル
  • spec
    • specディレクトリ配下にあるテストファイルがテストとして実行される
  • spec/spec_helper.rb
    • rspecの全体的な設定を記述するファイル
  • spec/rails_helper.rb
    • Rails特有の設定を記述するファイル

動作確認

テストを実行して動作を確認する。なお、テストコードはまだないため、なんのテストも実行されない。

実行コマンド

bundle exec rspec

実行結果

bundle exec rspec
No examples found.

Finished in 0.00022 seconds (files took 0.05756 seconds to load)
0 examples, 0 failures

参考

最後に

いかがでしたでしょうか。
今回はMacにRSpecの導入方法についてまとめてみました。
ここ違うよ!でしたり、こうした方がいいよ!などがあればコメントいただけると幸いです。

他にも下記のような記事を投稿しております。
興味がありましたら、ぜひご覧ください。

1
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
1
0