lekha pillai
lekha pillai

Reputation: 45

Factory girl object with has many through and presence validation

I have a model named Beats which consists of fields like name, status, group, owner. I am writing tests using R Spec and mocking using Factory Girl, I am facing issue running specs it gives error as ActiveRecord::RecordInvalid: Validation failed: Beat Types can't be blank, I am validating presence of all the fields of form, there is a dropdown with BeatType values, its also getting called when running specs, whats the way to include it in Factory of Beat?

class Beat < ActiveRecord::Base
has_many :beat_beat_types
has_many :beat_types, through: :beat_beat_types

validates :name,:status,:group,:owner,:beat_types presence: true, length: {maximum: 255}
end

class BeatType < ActiveRecord::Base
has_many :beat_beat_types
has_many :beats, through: :beat_beat_types
end

class BeatBeatType < ActiveRecord::Base
belongs_to :beat
belongs_to :beat_type
end


Factory_File of beat

FactoryGirl.define do
factory :beat do
  name
  status
  group
  owner
end
end

Upvotes: 0

Views: 313

Answers (1)

Manoj Menon
Manoj Menon

Reputation: 1038

Can you please try using

FactoryGirl.define do
  factory :beat do
  name
  status
  group
  owner
  beat_types { build_list :beat_type, 1 }
end

Upvotes: 1

Related Questions