2

I have the following model associations

class WarehouseVariant < AB
  has_one :warehouse
  has_one :variant
  has_many :variant_cities
end

class VariantCity < AB
  belongs_to :warehouse_variant
  delegate :mrp_paise, :to => :warehouse_variant
  validates :variant_id, uniqueness: { scope: :city_id }
end

The spec is the following:

let!(:warehouse_variant) {create(:warehouse_variant) }
subject { create(:variant_city, max_discount_percent: 99.99, max_promotion_percent: 99.99, warehouse_variant: warehouse_variant) }

it { is_expected.to validate_uniqueness_of(:variant_id).scoped_to(:city_id)}

The spec fails with the following error:

Module::DelegationError: VariantCity#mrp_paise delegated to warehouse_variant.mrp_paise, but warehouse_variant is nil: #

The thing is I inserted a debugging statement like this

it { binding.pry; is_expected.to validate_uniqueness_of(:variant_id).scoped_to(:city_id)}

and here the variant_city, has the object warehouse_variant, but when the test finishes I get the error mentioned above.

Any help would be appreciated.

EDIT:

The validates_presence_of(:variant_id) passes!!

MIdhun Krishna
  • 1,739
  • 1
  • 13
  • 31

0 Answers0