Skip to content

Instantly share code, notes, and snippets.

@willnet
Created December 21, 2016 02:49
Show Gist options
  • Save willnet/e25ae313544a3205b379c4ad4bd600f4 to your computer and use it in GitHub Desktop.
Save willnet/e25ae313544a3205b379c4ad4bd600f4 to your computer and use it in GitHub Desktop.
require 'test/unit'
class Person; end
class TestCheckedAttribute < Test::Unit::TestCase
def setup
add_checked_attribute(Person, :age) { |v| v >= 18 }
@bob = Person.new
end
def test_accepts_valid_values
@bob.age = 20
assert_equal 20, @bob.age
end
def test_refuses_invalid_values
assert_raises RuntimeError, 'Invalid attribute' do
@bob.age = 17
end
end
def add_checked_attribute(klass, attribute, &validation)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment