Sha256: a86b80d1cef747f5dae87d37774c8dc438f24fe02300a16a1f7c1f67f740d978

Contents?: true

Size: 1.2 KB

Versions: 1

Compression:

Stored size: 1.2 KB

Contents

# frozen_string_literal: true

require 'spec_helper'

RSpec.describe 'Attribute Default Value', type: :feature do
  context 'when given a default value' do
    class Meal
      include HappyMapper
      tag 'meal'
      attribute :type, String, default: 'omnivore'
    end

    let(:default_meal_type) { 'omnivore' }

    context 'when no value has been specified' do
      it 'returns the default value' do
        meal = Meal.parse('<meal />')
        expect(meal.type).to eq default_meal_type
      end
    end

    context 'when saving to xml' do
      let(:expected_xml) { %(<?xml version="1.0"?>\n<meal/>\n) }

      it 'the default value is not included' do
        meal = Meal.new
        expect(meal.to_xml).to eq expected_xml
      end
    end

    context 'when a new, non-nil value has been set' do
      let(:expected_xml) { %(<?xml version="1.0"?>\n<meal type="kosher"/>\n) }

      it 'returns the new value' do
        meal = Meal.parse('<meal />')
        meal.type = 'vegan'

        expect(meal.type).not_to eq default_meal_type
      end

      it 'saves the new value to the xml' do
        meal = Meal.new
        meal.type = 'kosher'
        expect(meal.to_xml).to eq expected_xml
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
nokogiri-happymapper-0.8.1 spec/features/attribute_default_value_spec.rb