Skip to content

Instantly share code, notes, and snippets.

@gstark
Created January 22, 2010 19:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gstark/284073 to your computer and use it in GitHub Desktop.
Save gstark/284073 to your computer and use it in GitHub Desktop.
require 'test/unit'
class Thingy
def method_missing(method_name,*args)
@attrs ||= {}
case method_name.to_s
when /^set_(.+)/ then
@attrs[$1] = args.first
when /^get_(.+)/ then
@attrs[$1]
else
super
end
end
end
class ThingyTest < Test::Unit::TestCase
def test_thingy_should_support_should_set_name
a = Thingy.new
a.set_name("Gavin")
end
def test_thingy_set_attr_should_only_take_first_arg
a = Thingy.new
a.set_name("Gavin", "is so cool")
assert a.get_name == "Gavin"
end
def test_thingy_should_support_should_set_address
a = Thingy.new
a.set_address("Home")
end
def test_thingy_should_suppport_get_name
a = Thingy.new
a.set_name("Gavin")
assert a.get_name == "Gavin"
end
def test_thingy_should_not_handle_set_without_an_attr
a = Thingy.new
assert_raise NoMethodError do
a.set_
end
end
def test_thingy_should_not_handle_get_without_an_attr
a = Thingy.new
assert_raise NoMethodError do
a.get_
end
end
def test_thingy_should_still_throw_method_missing_for_other_methods
a = Thingy.new
assert_raise NoMethodError do
a.wtf
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment