Help us understand the problem. What is going on with this article?

非ActiveRecordなテスト用データをFactoryGirlで生成する

More than 5 years have passed since last update.

非ActiveRecordなデータをletしたい

describe Something do
  let(:test_data) { FacotryGirl.build :chunky }
#...
  expect(test_data.name).to be('bacon')

と言う感じでActiveRecordにないテスト用のデータを生成したいと思ったので。

spec/support/models/test_data.rbでクラスを定義

spec/support/models/test_data.rb
class TestData
  attr_accessor :name, :anything_you_want
end

spec/factories/test_data.rbFactoryGirlを定義

spec/factories/test_data.rb
FactoryGirl.define do
  factory :chunky, class: :test_data do
    name 'bacon'
    anything_you_want 'hey!'
  end
end

で一番上に書いたような使い方が出来ます。

foloinfo
foloinfo / CEO at byus&co.ltd // Vimmer, 最近はめっきりReact
https://drip.ink
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away