Ruby on Rails開発のインターン (Day 18)

こんにちは、皆さん!

ついに今週もラスト1日!
昨日に引き続き、職場のプログラムでcapybaraテストが実行できるようにエラーの原因を探そうと思う。


  • トピック:
  1. Railsでdatabase cleanerを使う方法


  • 疑問:


  • 問題:
  1. capybaraテストが職場のプログラムで実行されない (解決した)


  • 学習した内容:
  1. テストの時に新しく追加されたデータを自動的に削除する方法


  • 今後やってみたいこと:
  1. Railsの'will-paginate'についてもっと学習する


  • リソース:
  1. GitHub - DatabaseCleaner/database_cleaner: Strategies for cleaning databases in Ruby. Can be used to ensure a clean state for testing.


Railsでdatabase cleanerを使う方法

毎回同じデータでテストを行うために、テスト環境のデータベースをもとの整理整頓された状態にキープしたいのはしばしばあることだ。というわけで、新しく作られたデータをお掃除する必要があるでしょう。

この目的のためにとても便利なジェムがある。そして、使い方も簡単。2ステップで完了。
***ここではRspecの場合だけについて言及する。CucumberとMinitestユーザーの方々すいません。***

最初に、ジェムのインストール。Gemfileに下記のを入れて、 bundle installコマンドを打ってください。

group :test do
  gem 'database_cleaner'
end

これをテストのグループに入れたのは同じデータをキープするためにデータベースのお掃除をするのはテスト環境の時だけでいいからだ。

次に、データベースお掃除の設定を書いていく。これは簡単で基本的には下記のをspec/spec_helper.rbファイルに入れればよい。

RSpec.configure do |config|

  config.before(:suite) do
    DatabaseCleaner.strategy = :transaction
    DatabaseCleaner.clean_with(:truncation)
  end

  config.around(:each) do |example|
    DatabaseCleaner.cleaning do
      example.run
    end
  end

end

この設定はdb/seeds.rbの状態に還元するというものだ。つまり、新しく追加されたデータのみが削除されるということだ。 これは便利!!

おまけ:
もし、capybaraテストも使っているのなら、上記に挙げた設定の代わりに、下記のものを使った方がいい。capybaraテストで作ったデータがテストされる前に削除されてしまう可能性があり、エラーの原因になりかねないからだ。
***必ずrequire 'capybara/rspec'の後に下記のを入れてください!***

RSpec.configure do |config|

  config.use_transactional_fixtures = false

  config.before(:suite) do
    if config.use_transactional_fixtures?
      raise(<<-MSG)
        Delete line `config.use_transactional_fixtures = true` from rails_helper.rb
        (or set it to false) to prevent uncommitted transactions being used in
        JavaScript-dependent specs.

        During testing, the app-under-test that the browser driver connects to
        uses a different database connection to the database connection used by
        the spec. The app's database connection would not be able to access
        uncommitted transaction data setup over the spec's database connection.
      MSG
    end
    DatabaseCleaner.clean_with(:truncation)
  end

  config.before(:each) do
    DatabaseCleaner.strategy = :transaction
  end

  config.before(:each, type: :feature) do
    # :rack_test driver's Rack app under test shares database connection
    # with the specs, so continue to use transaction strategy for speed.
    driver_shares_db_connection_with_specs = Capybara.current_driver == :rack_test

    unless driver_shares_db_connection_with_specs
      # Driver is probably for an external browser with an app
      # under test that does *not* share a database connection with the
      # specs, so use truncation strategy.
      DatabaseCleaner.strategy = :truncation
    end
  end

  config.before(:each) do
    DatabaseCleaner.start
  end

  config.append_after(:each) do
    DatabaseCleaner.clean
  end

end


まとめ

今日、職場のプログラムでcapybaraテストを実行することについに成功した!結果から言うと、ある小さなエラーのせいでほかの部分もうまくいっていなかったということだった。実につまらないもののせいでつまづいたなと思ったね。次はもっと気を付けていこうと思う。


ご精読ありがとうございました。では、また次回まで✌



Day 19はこちら↓↓
programming-shop.hatenablog.com


Day 17はこちら↓↓
programming-shop.hatenablog.com