6

プロジェクトで定義したいくつかのオブジェクトと関連付けのファクトリを作成しようとして問題が発生しました。私には循環的な種類の関連付けがあります。この場合、オブジェクトは、後で結合する他の2つのオブジェクトに関連付けられます。

+--------------+           +-------------+
|              |           |             |
| TestCase     +---------> | TestDataGrid|
|              |           |             |
+------+-------+           +------+------+
       |                          |
       |                          |
       |                          |
       v                          v
+--------------+           +--------------+
|              |           |              |
|              |           |              |
| TestVariable |           | TestDataSet  |
|              |           |              |
+------+-------+           +------+-------+
       |                          |
       |                          |
       |                          |
       |                          |
       |     +---------------+    |
       |     |               |    |
       |     |               |    |
       +---> | TestDataValue |<---+
             |               |
             +---------------+

class TestCase < ActiveRecord::Base
  has_many :test_variables, dependent: :destroy
  has_many :test_data_grids
  #...omitted code...
end

class TestVariable < ActiveRecord::Base
  belongs_to :test_case
  has_many :test_data_values
  #...omitted code...
end

class TestDataValue < ActiveRecord::Base
  belongs_to :test_variable
  belongs_to :test_data_set
  #...omitted code...
end

class TestDataSet < ActiveRecord::Base
  belongs_to :test_data_grid
  has_many :test_data_values
  #...omitted code...
end

class TestDataGrid < ActiveRecord::Base
  belongs_to :test_case
  has_many :test_data_sets
  #...omitted code...
end

基本的に、関連付けはTestCaseで分割され、TestDataValueで再び結合されます。同じオブジェクトで円を開閉するファクトリを作成するにはどうすればよいですか?

4

3 に答える 3

0

これはテストされていませんが、次のようになります。

FactoryGirl.define do

 factory :test_data_value do
  test_data_set
  test_variable
  # attributes
 end

 factory :test_data_set do
  test_data_grid
  # attributes
 end

 factory :test_variable do
  test_case
  # attributes
 end

 factory :test_case do
  # attributes
 end

 factory :test_data_grid do
  test_case
  # attributes
 end
end

次に仕様で:

@test_data_value = FactoryGirl.create(:test_data_value)

@test_variable = @test_data_value.test_variable
@test_data_set = @test_data_value.test_data_set
于 2012-09-07T17:04:56.937 に答える
0

手動で行うことができます:

test_case = FactoryGirl.build(:test_case)

test = FactoryGirl.build(:test_data_value,
  :test_variable => FactoryGirl.build(:test_variable,
     :test_case => test_case
  ),
  :test_data_set => FactoryGirl.build(:test_data_set,
    :test_data_grid => FactoryGirl.build(:test_data_grid,
       :test_case => test_case
    )
  )
)

test.test_variable.test_case == test.test_data_set.test_data_grid.test_case
# => true

または、いくつかのヘルパー ファクトリを記述します。

FactoryGirl.define do
  factory :test_data_value do
    ignore do
      test_case nil
      test_data_grid nil
    end

    test_variable do
      build :test_variable,
        :test_case => test_case 
                      || test_data_grid.try(:test_case) 
                      || build(:test_case)
    end

    test_data_set do
      build :test_data_set, :test_data_grid => test_data_grid || (
        build :test_data_grid, :test_case => test_case || build(:test_case)
      )
    end
  end
end

共通の祖先としてのテスト ケース:

test = FactoryGirl.create :test_data_value, 
  :test_case => FactoryGirl.build(:test_case)

test.test_variable.test_case == test.test_data_set.test_data_grid.test_case
# => true

共通の祖先としてのテスト ケースと、各インスタンスの同じ test_data_grid:

test_data_grid = FactoryGirl.build :test_data_grid, 
  :test_case => FactoryGirl.build(:test_case)

test = FactoryGirl.create :test_data_value, 
  :test_data_grid => test_data_grid

test.test_variable.test_case == test.test_data_set.test_data_grid.test_case
# => true
于 2012-09-16T20:36:36.233 に答える
0

私の提案は、工場ではなく、テスト自体に設定することです.

ファクトリ ファイル (@veritas1 に感謝)

FactoryGirl.define do

  factory :test_data_value do
    # attributes
  end

  factory :test_data_set do
    # attributes
  end

  factory :test_variable do
    # attributes
  end

  factory :test_case do
    # attributes
  end

  factory :test_data_grid do
    # attributes
  end
end

テストファイル

@test_case = FactoryGirl.create(:test_case)
@test_data_grid = FactoryGirl.create(:test_case)
@test_variable = FactoryGirl.create(:test_case)
@test_data_set = FactoryGirl.create(:test_case)
@test_data_value = FactoryGirl.create(:test_case)
@test_case.test_data_grids << @test_data_grid
@test_case.test_variables << @test_variable
@test_data_grid.test_data_set << @test_data_set
@test_variable.test_data_values << @test_data_value
@test_data_set.test_data_values << @test_data_value

少し面倒かもしれませんが、それを行う方法のように思えます。ただし、考えてみれば、テストに苦労している場合は、通常、今後苦労する兆候であり、API を再設計する必要があります。別の方法はわかりませんが、ドメインの知識があればできるかもしれません。

于 2012-09-11T14:39:13.063 に答える