2016-01-12 18:43:00 -06:00
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
RSpec.describe Ingredient, type: :model do
|
2016-01-14 15:22:15 -06:00
|
|
|
|
|
|
|
describe 'validation' do
|
|
|
|
|
|
|
|
it 'validates density' do
|
|
|
|
i = build(:ingredient)
|
|
|
|
expect(i).to be_valid
|
|
|
|
|
|
|
|
i.density = '5'
|
|
|
|
expect(i).not_to be_valid
|
|
|
|
|
|
|
|
i.density = '5 cup'
|
|
|
|
expect(i).not_to be_valid
|
|
|
|
|
|
|
|
i.density = '5 gram/cup'
|
|
|
|
expect(i).to be_valid
|
|
|
|
|
|
|
|
i.density = '5 mile/hour'
|
|
|
|
expect(i).not_to be_valid
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2016-02-02 15:48:20 -06:00
|
|
|
describe 'set_usda_food' do
|
|
|
|
it 'sets the density' do
|
|
|
|
i = build(:ingredient)
|
|
|
|
f = create(:salted_butter)
|
2016-07-05 16:31:36 -05:00
|
|
|
create(:usda_food_weight, usda_food: f)
|
2016-02-02 15:48:20 -06:00
|
|
|
|
|
|
|
i.set_usda_food(f)
|
|
|
|
|
|
|
|
expect(i.density).not_to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-07-05 16:31:36 -05:00
|
|
|
describe '#custom_unit_weight' do
|
|
|
|
it 'returns a ValueUnit for valid custom weights' do
|
|
|
|
i = build(:ingredient)
|
|
|
|
i.ingredient_units << IngredientUnit.new(name: 'clove', gram_weight: 20.0)
|
|
|
|
|
|
|
|
vu = i.custom_unit_weight('clove')
|
|
|
|
expect(vu).not_to be_nil
|
|
|
|
expect(vu.raw_value).to eq 20.0
|
|
|
|
expect(vu.unit.to_s).to eq 'gram'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-01-12 18:43:00 -06:00
|
|
|
end
|