82 lines
2.6 KiB
Ruby
82 lines
2.6 KiB
Ruby
require 'rails_helper'
|
|
|
|
RSpec.describe RecipeIngredient, type: :model do
|
|
|
|
describe 'to_mass' do
|
|
|
|
it 'converts volume ingredients with density' do
|
|
ri = RecipeIngredient.new(quantity: 2, units: 'tbsp', food: create(:food_with_density))
|
|
expect(ri.as_value_unit.mass?).to be_falsey
|
|
ri.to_mass
|
|
expect(ri.as_value_unit.mass?).to be_truthy
|
|
end
|
|
|
|
it 'converts ingredients with custom units' do
|
|
i = create(:food_with_density)
|
|
i.food_units << FoodUnit.new(name: 'pat', gram_weight: 25)
|
|
ri = RecipeIngredient.new(quantity: 2, units: 'pat', food: i)
|
|
ri.to_mass
|
|
vu = ri.as_value_unit
|
|
expect(vu.raw_value).to eq 50
|
|
expect(vu.unit.to_s).to eq 'gram'
|
|
end
|
|
|
|
end
|
|
|
|
describe '#can_convert_to_grams' do
|
|
describe 'with no ingredient detail' do
|
|
it 'returns false if no quantity or unit' do
|
|
ri = RecipeIngredient.new
|
|
expect(ri.can_convert_to_grams?).to be_falsey
|
|
end
|
|
|
|
it 'returns false if no quantity' do
|
|
ri = RecipeIngredient.new(units: 'lbs')
|
|
expect(ri.can_convert_to_grams?).to be_falsey
|
|
end
|
|
|
|
it 'returns false if no units' do
|
|
ri = RecipeIngredient.new(quantity: '5 1/2')
|
|
expect(ri.can_convert_to_grams?).to be_falsey
|
|
end
|
|
|
|
it 'returns false if weird units' do
|
|
ri = RecipeIngredient.new(quantity: '5 1/2', units: 'dogs')
|
|
expect(ri.can_convert_to_grams?).to be_falsey
|
|
end
|
|
|
|
it 'returns true if unit is mass' do
|
|
ri = RecipeIngredient.new(quantity: '5 1/2', units: 'lbs')
|
|
expect(ri.can_convert_to_grams?).to be_truthy
|
|
end
|
|
|
|
it 'returns false if unit is volume' do
|
|
ri = RecipeIngredient.new(quantity: '5 1/2', units: 'cups')
|
|
expect(ri.can_convert_to_grams?).to be_falsey
|
|
end
|
|
end
|
|
|
|
describe 'with ingredient' do
|
|
it 'returns false if unit is volume and ingredient has no density' do
|
|
ri = RecipeIngredient.new(quantity: '5 1/2', units: 'cups', food: create(:food))
|
|
expect(ri.can_convert_to_grams?).to be_falsey
|
|
end
|
|
|
|
it 'returns true if unit is volume and ingredient has density' do
|
|
ri = RecipeIngredient.new(quantity: '5 1/2', units: 'cups', food: create(:food_with_density))
|
|
expect(ri.can_convert_to_grams?).to be_truthy
|
|
end
|
|
end
|
|
|
|
describe 'with recipe_as_ingredient' do
|
|
it 'return true if unit is volume and recipe has density' do
|
|
ri = RecipeIngredient.new(quantity: '5 1/2', units: 'cups', recipe_as_ingredient: create(:recipe, yields: '4 cups, 13 oz'))
|
|
expect(ri.can_convert_to_grams?).to be_truthy
|
|
end
|
|
end
|
|
|
|
|
|
end
|
|
|
|
end
|