tap/spec/models/product_spec.rb

115 lines
2.6 KiB
Ruby
Raw Normal View History

# == Schema Information
#
# Table name: products
#
# id :integer not null, primary key
# name :string not null
# price_cents :integer default("0"), not null
2015-02-09 17:06:24 +01:00
# created_at :datetime
# updated_at :datetime
# avatar_file_name :string
# avatar_content_type :string
# avatar_file_size :integer
# avatar_updated_at :datetime
# category :integer default("0")
# stock :integer default("0"), not null
# calories :integer
# deleted :boolean default("f")
#
describe Product do
before :each do
@product = create :product
end
2015-03-03 17:41:53 +01:00
it 'has a valid factory' do
expect(@product).to be_valid
2015-03-03 17:41:53 +01:00
end
2015-09-03 23:12:20 +02:00
2015-09-21 08:40:15 +02:00
############
# FIELDS #
############
describe 'fields' do
describe 'name' do
it 'should be present' do
@product.name = nil
expect(@product).to_not be_valid
end
it 'shold be unique' do
expect(build :product, name: @product.name).to_not be_valid
end
2015-09-03 23:12:20 +02:00
end
2015-09-21 08:40:15 +02:00
describe 'price_cents' do
it 'should be present' do
@product.price_cents = nil
2015-09-03 23:12:20 +02:00
expect(@product).to_not be_valid
end
2015-09-21 08:40:15 +02:00
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
2015-09-03 23:12:20 +02:00
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
2015-09-21 08:40:15 +02:00
@product.stock = 0
expect(@product).to be_valid
2015-09-03 23:12:20 +02:00
end
end
describe 'calories' do
2015-09-21 08:40:15 +02:00
it 'does not have to be present' do
2015-09-03 23:12:20 +02:00
@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
2015-09-21 08:40:15 +02:00
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)
2015-09-03 23:12:20 +02:00
end
end
2015-09-21 08:40:15 +02:00
2014-11-24 21:45:32 +01:00
end