Add tests for product validations

This commit is contained in:
benji 2015-09-03 23:12:20 +02:00
parent bb76b36d86
commit 168b723dce
2 changed files with 51 additions and 1 deletions

View file

@ -25,7 +25,7 @@ class Product < ActiveRecord::Base
validates :name, presence: true
validates :price_cents, numericality: { only_integer: true, greater_than_or_equal_to: 0 }
validates :stock, numericality: { only_integer: true, greater_than_or_equal_to: 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 }
validates_attachment :avatar,
presence: true,

View file

@ -25,4 +25,54 @@ describe Product do
it 'has a valid factory' 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
describe 'price' do
it 'should be positive' do
@product.price = -5
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)
end
end
describe 'stock' do
it 'should be present' do
@product.stock = nil
expect(@product).to_not be_valid
end
it 'should be positive' do
@product.stock = -5
expect(@product).to_not be_valid
end
end
describe 'calories' do
it 'should not be present' do
@product.calories = nil
expect(@product).to be_valid
end
it 'should be positive' do
@product.calories = -5
expect(@product).to_not be_valid
end
end
it 'avatar should be present' do
@product.avatar = nil
expect(@product).to_not be_valid
end
end
end