tap/spec/models/product_spec.rb

142 lines
3.2 KiB
Ruby
Raw Normal View History

# == Schema Information
#
# Table name: products
#
# id :integer not null, primary key
# name :string not null
2016-03-08 15:53:50 +01:00
# 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
2016-03-08 15:53:50 +01:00
# category :integer default(0)
# stock :integer default(0), not null
# calories :integer
2016-03-08 15:53:50 +01:00
# deleted :boolean default(FALSE)
#
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
2015-10-28 22:58:56 +01:00
it 'should be a number' do
@product.stock = "123abc"
expect(@product).to_not be_valid
end
2015-09-03 23:12:20 +02:00
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
2015-10-28 22:58:56 +01:00
it 'should be a number' do
@product.calories = "123abc"
expect(@product).to_not be_valid
end
2015-09-03 23:12:20 +02:00
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
2015-10-28 22:58:56 +01:00
describe 'deleted' do
it 'should default false' do
expect(@product.deleted).to be false
end
end
2015-09-21 08:40:15 +02:00
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
2015-10-28 23:34:03 +01:00
############
# SCOPES #
############
2015-10-28 22:58:56 +01:00
describe 'for sale' do
it 'should return non-deleted products' do
product = create :product
product.update_attribute(:deleted, true)
expect(Product.for_sale).to eq([@product])
end
end
2014-11-24 21:45:32 +01:00
end