factory_bot / factory_girl语法,用于添加belongs_to

时间:2018-07-11 23:04:15

标签: ruby-on-rails factory-bot

我有以下模型:

public ArrayList<Song> getSongsFromAlbum(long ID) {
  ArrayList<Song> songs = new ArrayList<>();
  ContentResolver cr = getApplicationContext().getContentResolver();
  Cursor cursor = cr.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null);
  ...loop through and get songs, create Song object add it to songs array;
  return songs;
}

以及以下带有注释问题的工厂:

class SecondaryIabCategory < ApplicationRecord
  has_many :sites, foreign_key: :secondary_category_id
  belongs_to :primary_iab_category
end

我收到错误消息:

  factory :primary_category, class: PrimaryIabCategory do
    name 'Arts & Entertainment - yaddah'
    value 'IAB1-some'
  end

  factory :another_primary_category, class: PrimaryIabCategory do
    name 'Games are cool! - dabbah'
    value 'IAB1-other'
  end

  factory :secondary_category, class:  SecondaryIabCategory do
    # here is the problem - what's the correct syntax?
    primary_iab_category another_primary_category
    name 'Test Secondary Cat'
    value 'xxyy'
  end

我已经有几个月没有使用FactoryBot / Girl了,所以不确定其语法是什么。有什么想法吗?

1 个答案:

答案 0 :(得分:1)

尝试一下

factory :secondary_category, class:  SecondaryIabCategory do
  association :primary_iab_category, factory: :another_primary_category
  name 'Test Secondary Cat'
  value 'xxyy'
end

rubydoc很漂亮