Sha256: 15d3618cd28992a6f5cfd7267df068c0ef1cbaa546935b6031c07f9d93cd6c1b

Contents?: true

Size: 1.51 KB

Versions: 2

Compression:

Stored size: 1.51 KB

Contents

require 'minitest_helper'

describe Setting do
  before do
    Setting.druthers_cache.clear
    Setting.delete_all
  end

  describe 'validations' do
    def assert_saved_and_valid(key)
      Setting.where(key: key).first.valid?.must_be_true
    end

    it 'works on defined settings without a validation' do
      Setting.quest = 'to eat bacon in moderation'
      assert_saved_and_valid('quest')
    end

    it 'works on defined settings with a validation' do
      Setting.favourite_colour = 'green'
      assert_saved_and_valid('favourite_colour')
    end

    it 'raises validation errors correctly' do
      proc { Setting.favourite_colour = 'invalid' }.
        must_raise ActiveRecord::RecordInvalid
    end

    it 'allows unknown settings' do
      Setting.create do |ea|
        ea.key = 'airspeed'
      end.valid?.must_be_true
    end
  end

  describe 'defaults' do
    it 'returns proper defaults' do
      Setting.quest.must_equal 'to find the holy grail'
      Setting.favourite_colour.must_be_nil
      Setting.things.must_equal [1, 2, 3]
    end
  end

  describe 'serialized persistence' do
    it "works with arrays" do
      a = [3, 4, 5]
      Setting.things = a
      Setting.where(key: 'things').first.value.must_equal a
    end

    it "works with hashes" do
      h = {hello: 'world'}
      Setting.hashish = h
      Setting.where(key: 'hashish').first.value.must_equal h
    end
    
    it 'returns serialized value correctly' do
      Setting.change = 'foo'
      Setting.change.must_equal 'bar'
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
druthers-1.0.0 test/setting_test.rb
druthers-0.0.3 test/setting_test.rb