51 lines
1.0 KiB
Ruby
51 lines
1.0 KiB
Ruby
require 'rails_helper'
|
|
|
|
RSpec.describe Food, type: :model do
|
|
|
|
describe 'validation' do
|
|
|
|
it 'validates density' do
|
|
i = build(:food)
|
|
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(:food)
|
|
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_units' do
|
|
it 'returns a hash based on food_units' do
|
|
i = build(:food)
|
|
i.food_units << FoodUnit.new(name: 'clove', gram_weight: 20.0)
|
|
|
|
units = i.custom_units
|
|
expect(units['clove']).to be_a UnitConversion::ValueUnit
|
|
expect(units['clove'].value.value).to eq 20.0
|
|
expect(units['clove'].unit.unit).to eq 'g'
|
|
end
|
|
end
|
|
|
|
end
|