LoginSignup
6
4

More than 5 years have passed since last update.

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

Posted at

非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

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

6
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
4