require 'rails_helper' RSpec.describe Ingredient, type: :model do 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 describe 'set_usda_food' do it 'sets the density' do i = build(:ingredient) f = create(:salted_butter) create(:usda_food_weight, usda_food: f) i.set_usda_food(f) expect(i.density).not_to be_nil end end 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 end