There are several ways, depending on how it's defined. If you defined it normally (somehow def
or define_method
in an eval
or class_eval
), then you can use defined? obj.prop
to ensure that it responds to a reader (there's no equivalent for a writer). You can also use obj.respond_to? :prop
for the reader, and obj.respond_to? :prop=
for the writer.
If you're using method_missing
to mimic a call for the property, then obj.respond_to?
will only work if you redefined that as well (in which case you need to test it separately), and the only way to test your property is to try reading from it and writing to it, and asserting that it doesn't throw any exceptions.