Edit product test

This commit is contained in:
benji 2015-09-21 08:40:15 +02:00
parent a28c89469e
commit e9d89c1e95
2 changed files with 65 additions and 22 deletions

View file

@ -24,7 +24,7 @@ class Product < ActiveRecord::Base
enum category: %w(food beverages other)
validates :name, presence: true
validates :name, presence: true, uniqueness: true
validates :price_cents, presence: true, numericality: { only_integer: true, greater_than: 0 }
validates :stock, presence: true, numericality: { only_integer: true, greater_than_or_equal_to: 0 }
validates :calories, numericality: { only_integer: true, allow_nil: true, greater_than_or_equal_to: 0 }
@ -40,8 +40,4 @@ class Product < ActiveRecord::Base
if value.is_a? String then value.sub!(',', '.') end
self.price_cents = (value.to_f * 100).to_int
end
def take_out_of_sale!
update_attribute :deleted, true
end
end

View file

@ -26,23 +26,38 @@ describe Product do
expect(@product).to be_valid
end
describe 'validations' do
it 'name should be present' do
@product.name = ''
expect(@product).to_not be_valid
end
############
# FIELDS #
############
describe 'price' do
it 'should be positive' do
@product.price = -5
describe 'fields' do
describe 'name' do
it 'should be present' do
@product.name = nil
expect(@product).to_not be_valid
end
it 'should be saved correctly' do
@product.price = 1.20
@product.save
expect(@product.reload.price).to eq(1.20)
expect(@product.reload.price_cents).to eq(120)
it 'shold be unique' do
expect(build :product, name: @product.name).to_not be_valid
end
end
describe 'price_cents' do
it 'should be present' do
@product.price_cents = nil
expect(@product).to_not be_valid
end
it 'should be a number' do
@product.price_cents = "123abc"
expect(@product).to_not be_valid
end
it 'should be strict positive' do
@product.price = -5
expect(@product).to_not be_valid
@product.price = 0
expect(@product).to_not be_valid
end
end
@ -55,11 +70,13 @@ describe Product do
it 'should be positive' do
@product.stock = -5
expect(@product).to_not be_valid
@product.stock = 0
expect(@product).to be_valid
end
end
describe 'calories' do
it 'should not be present' do
it 'does not have to be present' do
@product.calories = nil
expect(@product).to be_valid
end
@ -70,9 +87,39 @@ describe Product do
end
end
it 'avatar should be present' do
@product.avatar = nil
expect(@product).to_not be_valid
describe 'barcode' do
it 'should be present' do
@product.barcode = nil
expect(@product).to_not be_valid
end
it 'should be unique' do
expect(build :product, barcode: @product.barcode).to_not be_valid
end
end
describe 'avatar' do
it 'should be present' do
@product.avatar = nil
expect(@product).to_not be_valid
end
end
end
#############
# METHODS #
#############
describe 'price' do
it 'should read the correct value' do
expect(@product.price).to eq(@product.price_cents / 100.0)
end
it 'should write the correct value' do
@product.price = 1.5
@product.save
expect(@product.reload.price_cents).to eq(150)
end
end
end