summaryrefslogtreecommitdiff
path: root/spec
diff options
context:
space:
mode:
authorHombreLaser <sebastian-440@live.com>2023-04-14 20:49:46 -0600
committerHombreLaser <sebastian-440@live.com>2023-04-14 20:49:46 -0600
commit8fbeea986d06a0052ce0132717f22b6ff8bd1a04 (patch)
tree75cd090f680853574ce36865089cb2775ed0fe75 /spec
parent4b0527efc4b7703c99043013fd455f496d32329c (diff)
Añade validador de existencias a ProductCart
Diffstat (limited to 'spec')
-rw-r--r--spec/factories/product_cart.rb2
-rw-r--r--spec/models/product_cart_spec.rb10
2 files changed, 11 insertions, 1 deletions
diff --git a/spec/factories/product_cart.rb b/spec/factories/product_cart.rb
index 713fd0a..95094dd 100644
--- a/spec/factories/product_cart.rb
+++ b/spec/factories/product_cart.rb
@@ -3,7 +3,7 @@
FactoryBot.define do
factory :product_cart, class: 'ProductCart' do
cart { create(:cart) }
- product { create(:product) }
+ product { create(:product, available_quantity: 2000) }
quantity { rand(1..1000) }
end
end
diff --git a/spec/models/product_cart_spec.rb b/spec/models/product_cart_spec.rb
index 2c4d9d1..46860d3 100644
--- a/spec/models/product_cart_spec.rb
+++ b/spec/models/product_cart_spec.rb
@@ -20,4 +20,14 @@ RSpec.describe ProductCart, type: :model do
expect(another_product_cart.errors[:product_id]).to be_present
end
end
+
+ describe '#product_quantity' do
+ it "validates the requested quantity against the product's available quantity" do
+ product = create(:product, available_quantity: 5)
+ another_product_cart = build(:product_cart, cart: product_cart.cart, product:, quantity: 10)
+
+ expect(another_product_cart.save).to be_falsey
+ expect(another_product_cart.errors[:product_id][0]).to eq('not enough in existence')
+ end
+ end
end