tusbasaのブログ

業務や勉強中に調べたことを自分用にメモするブログ

【rspec】 before

beforeは一番外側のdescribeの内側のcontextdescribeの中でしか動作しない

以下のbeforeは一番外側のdescribeの直下にbeforeがある為動作しない

RSpec.describe 'スレッド作成', type: :system do

    before do
      user = FactoryBot.create(:user, name: 'sample',
        email: 'sample@example.com',
        password: 'password')
      visit new_user_session_path
      fill_in "メールアドレス", with: "sample@example.com"
      fill_in "パスワード", with: "password"
      click_button "ログイン"
    end

    it "スレッドを作成できること" do
      categories = FactoryBot.create :category, :work
      visit new_post_path
      fill_in "タイトル", with: "ruby"
      fill_in "説明", with: "ruby"
      check categories.name
      click_button "作成"
      expect(page).to have_content "作成完了"
    end

    it "カテゴリーを選択しない場合スレッドを作成できないこと" do
      categories = FactoryBot.create :category, :work
      visit new_post_path
      fill_in "タイトル", with: "ruby"
      fill_in "説明", with: "ruby"
      click_button "作成"
      expect(page).to have_content "カテゴリーを入力してください"
    end
end

以下のbeforeは一番外側のdescribeの中のcontextに中にbeforeがある為動作する

RSpec.describe 'スレッド作成', type: :system do

  context "スレッド作成について" do

    before do
      user = FactoryBot.create(:user, name: 'sample',
        email: 'sample@example.com',
        password: 'password')
      visit new_user_session_path
      fill_in "メールアドレス", with: "sample@example.com"
      fill_in "パスワード", with: "password"
      click_button "ログイン"
    end

    it "スレッドを作成できること" do
      categories = FactoryBot.create :category, :work
      visit new_post_path
      fill_in "タイトル", with: "ruby"
      fill_in "説明", with: "ruby"
      check categories.name
      click_button "作成"
      expect(page).to have_content "作成完了"
    end

    it "カテゴリーを選択しない場合スレッドを作成できないこと" do
      categories = FactoryBot.create :category, :work
      visit new_post_path
      fill_in "タイトル", with: "ruby"
      fill_in "説明", with: "ruby"
      click_button "作成"
      expect(page).to have_content "カテゴリーを入力してください"
    end
  end
end