1

テスト データベースのシード データにいつでもアクセスできるようにしたいと考えています。そのように設定されている場合、database_cleaner がすべてを削除することを理解しています。

すべてを削除してからシードをリロードしようとしましたが、テストで js: true を使用しようとすると、シードがロードされないため、データが存在しないというエラーが発生します。

私のspec_helper.rb

RSpec.configure do |config|
  # before the entire test suite runs, clear the test database out completely
  config.before(:suite) do
    DatabaseCleaner.clean_with(:truncation)
  end
  # sets the default database cleaning strategy to be transactions (very fast)
  config.before(:each) do
    DatabaseCleaner.strategy = :transaction
  end
  # For these types of tests, transactions won’t work. We must use truncation
  config.before(:each, js: true) do
    DatabaseCleaner.strategy = :truncation
  end
  # hook up database_cleaner around the beginning and end of each test, telling it to execute whatever cleanup strategy we selected beforehand.
  config.before(:each) do
    DatabaseCleaner.start
  end
  config.after(:each) do
    DatabaseCleaner.clean
  # reload the seed so we have data to play with
  end
  config.before :all do
    Rails.application.load_seed
  end
end

私のview_specにあるとき、私はこのようなものを持っています

require 'spec_helper'
describe 'my/path', type: :view do
  before do
    @user = create(:user)
    @user.permissions << Permission.first
    login_as(@user)
    visit my_path
  end
  it 'should have a valid user, just for kicks' do
    @user.should be_valid
  end
  it 'should be in the path i said' do
    expect(current_path).to eq(my_path)
  end
  describe 'click submit button', js: true do
    it 'should take me to a different path' do
      click_link('button_1')
      expect(current_path).to eq(my_new_path)
    end
  end
end

最初の 2 つのテストは実行され、そのユーザーを作成しても問題ありませんが、js: true で最後のテストに到達するとすぐに、データベースにアクセス許可がなくなります。

rspec によって追加されたデータのみを削除するように database_cleaner に指示する方法はありますか? 種ではありませんか?または、特定のテーブルを削除しないように指示することもできますか?

どんな助けでも大歓迎です。

4

1 に答える 1