0

これが私のrspecファイルです:

require 'spec_helper'

describe Classroom, focus: true do

  describe "associations" do
    it { should belong_to(:user) }
  end

  describe "validations" do
    it { should validate_presence_of(:user) }
  end

  describe "instance methods" do

    describe "archive!" do
      before(:each) do
        @classroom = build_stubbed(:classroom)
      end

      context "when a classroom is active" do
        it "should mark classroom as inactive" do
          @classroom.archive!
          @classroom.active.should_be == false
        end
      end

    end

  end

end

これが私のものClassroom Factoryです:

FactoryGirl.define do

  factory :classroom do
    name "Hello World"
    active true

    trait :archive do
      active false
    end
  end

end

上記のインスタンス メソッド テストを実行すると、次のエラーが表示されます。stubbed models are not allowed to access the database

なぜこれが起こっているのかは理解していますが(テストの知識が不足しているため/テストの初心者であるため)、データベースにヒットしないようにモデルをスタブ化する方法がわかりません

ワーキング Rspec テスト:

require 'spec_helper'

describe Classroom, focus: true do

  let(:classroom) { build(:classroom) }

  describe "associations" do
    it { should belong_to(:user) }
  end

  describe "validations" do
    it { should validate_presence_of(:user) }
  end

  describe "instance methods" do

    describe "archive!" do

      context "when a classroom is active" do
        it "should mark classroom as inactive" do
          classroom.archive!
          classroom.active == false
        end
      end

    end

  end

end
4

1 に答える 1