FactoryGirl.create не работает при обновлении версии mongoid с 5 до 6. Ниже приведена проблема, с которой я столкнулся при запуске теста rspec
Это место, где создается тестовая таблица:
factory :reward_scheme, class: RewardsModels::RewardScheme do
uid { ExpectedData::COSTA_UID }
scheme_type { "bricks" }
reward_type { "menu"}
company_address { FactoryGirl.build(:company_address) }
reward_config { FactoryGirl.build(:reward_config) }
brand { FactoryGirl.build(:brand) }
master_offers { [ FactoryGirl.build(:master_offer) ] }
master_specials { [ FactoryGirl.build(:master_special) ] }
url "http://costa.com"
after(:create) do |reward_scheme|
reward_scheme.stores << FactoryGirl.create(:store)
reward_scheme.user_cards << FactoryGirl.create(:user_card)
end
end
Логи следующие:
CoreModels:: Ошибка транзакции / ошибка: reward_scheme.stores << FactoryGirl.create(:store)
Mongoid::Errors::Validations:
message:
Validation of RewardsModels::Store failed.
summary:
The following errors were found: Reward scheme can't be blank
resolution:
Try persisting the document with valid data or remove the validations.
# ./spec/factories/reward_scheme.rb:15:in `block (3 levels) in <top (required)>'
# ./spec/core/transaction_spec.rb:6:in `block (2 levels) in <top (required)>'
Вот как выглядит файл модели:
модуль UserModels
Класс Store включает Mongoid:: Документ включает Mongoid::Timestamps
field :reward_scheme_id, type: String
field :store_id, type: String
field :store_name, type: String, default: "HQ"
field :reward_scheme_name, type:String
field :about, type: String, default: "MyGravity - loyalty begins with trust"
field :logo, type: String, default: 'https://static.mygravity.co/partners/logo.svg'
field :splash_screen_url, type: String, default: "https://static.mygravity.co/assets/SplitShire_Blur_Background_XVI.jpg"
field :awaiting_update, type: Boolean, default:false
embeds_one :location, class_name:'UserModels::Location'
embeds_one :open_hours, class_name:'UserModels::OpenHours'
embeds_one :optional, class_name:'UserModels::Optional'
embeds_many :specials, class_name:'UserModels::Special'
embeds_many :offers, class_name:'UserModels::Offer'
before_create :set_defaults
def set_defaults
self.location = UserModels::Location.new unless self.location
self.optional = UserModels::Optional.new unless self.optional
end
конец
Расположение класса включает Mongoid::Document
field :longitude, type: Float, default: -0.131425
field :latitude, type: Float, default: 51.507697
field :address_line_1, type: String, default: 'Impact Hub - Westmister'
field :post_code, type: String, default: 'SW1Y 4TE'
field :city_town, type: String, default: 'London'
embedded_in :store
конец
Класс OpenHours включает Mongoid::Document
field :monday, type: String
field :tuesday, type: String
field :wednesday, type: String
field :thursday, type: String
field :friday, type: String
field :saturday, type: String
field :sunday, type: String
field :sunday_1, type: String
embedded_in :store
конец
Класс Special включает Mongoid::Document
# Need this to search
field :special_id, type: Integer
field :special_uid, type: Integer
field :title, type: String
field :text, type: String
embedded_in :store
before_save :set_special_uid
def set_special_uid
self.special_uid = self.special_id
end
def attributes
# p super
# p Hash[super.map{|k,v| [(alais[k] || k), v]}]
hash = super
alais = {'special_id' => 'id'}
hash.keys.each do |k,v|
hash[ alais[k] ] = hash['special_id'].to_s if alais[k]
# Need this as special_id is mapped in the iOS to a string...
hash['special_id'] = hash['special_id'].to_s if k == 'special_id'
end
hash
end
конец
Предложение класса включает Mongoid:: Документ
field :name, type: String
field :offer_id, type: Integer
field :value, type: Float, default:0.0 # monetary value
field :points, type: Integer
field :icon_url, type: String
field :icon_name, type: String
embedded_in :store
def attributes
# p super
# p Hash[super.map{|k,v| [(alais[k] || k), v]}]
hash = super
alais = {'offer_id' => 'id'}
hash.keys.each { |k,v| hash[ alais[k] ] = hash['offer_id'] if alais[k] }
hash
end
конец
Класс Необязательный include Mongoid::Document
field :email, type: String, default:""
field :twitter, type: String, default:""
field :telephone, type: String, default:""
field :wifi_password, type: String, default:""
embedded_in :store
конец конец
Любые предложения относительно изменений кода, требуемых для обновления до mongoid 6, высоко ценятся. Спасибо