Rspec let!を使用する方法はありますか?スコープを使用すると、複数の例で使用できるようになりますか?

概要

私は、一連のテストの後にクリーンアップされるが、個々のテストの後ではクリーンアップされない rspec のメモ化された変数の設定を処理する良い方法を見つけようとしています。

次のようなことを達成したいと考えていますが、let または同様のものを使用して、後で状態を元に戻す責任を負う必要がありません。

RSpec.describe person do
  before(:context) do
    @shared_value = create(:person)
  end
  after(:context) do
    @shared_value.destroy
  end

  it "test 1" do
    expect(@shared_value).to be_a(Person)
  end

  it "test 2" do
    expect(@shared_value).to be_a(Person)
  end
end

次のコード スニペットのような単純な構文でこれを解決したいと思います。注: これによりエラーが発生することは承知しています。同様に、let! の問題も同様です。メソッドに関しては、各例のスコープのみが提供されるということです。

  let(:shared_person)
  before(:all)
    shared_person
  end
  it "test 1" do
    expect(shared_person).to be_a(Person)
  end

シンプル/直感的な構文でこれを実現するネイティブ オプション、Gem、またはライブラリはありますか?

解決策

test_prof gem、let_it_be 機能を使用します。

https://translate.google.com/translate?hl=ja&sl=en&tl=ja&u=https://github.com/test-prof/test-prof/blob/master/docs/recipes/let_it_be.md

が推奨します https://translate.google.com/translate?hl=ja&sl=en&tl=ja&u=https://github.com/rspec/rspec-core/issues/3065