<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div><div>On 22 Nov 2011, at 19:41, Tatsuya Ono wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><div>Other solution is to use instance_variable_get and instance_variable_set methods.</div><div><a href="http://ruby-doc.org/core-1.9.3/Object.html#method-i-instance_variable_get">http://ruby-doc.org/core-1.9.3/Object.html#method-i-instance_variable_get</a></div>
<div><a href="http://ruby-doc.org/core-1.9.3/Object.html#method-i-instance_variable_set">http://ruby-doc.org/core-1.9.3/Object.html#method-i-instance_variable_set</a></div><div><br></div><div><div>However I would use attr_accessor instead of put/get method in this case. I do not write spec for each attributes usually to be honest, but we could also write a custom matcher[1] for attr_accessor if needed. Both implementation and its spec will be clean like below [2].</div></div><div><br></div><div>Cheers,</div><div><br></div>
<div>Tatsuya</div></blockquote><br></div><div><br></div>Fairly sure this is a contrived/simplified example for discussion, in which case discussing specific approaches to the solution may be more than a little academic, but…<div><br></div><div>Using a custom matcher to assert that attr_accessor is defined feels dodgy to me. Regardless of the matcher's implementation, which will probably just check that Subject#property and Subject#property=() are defined and work as expected, using this matcher in the test code strongly implies a specific implementation. Thus, psychologically, it will discourage changing the class under test not to use attr_accessor, and we want to specify behaviour rather than implementation.</div><div><br></div><div>This is the paragraph where I restrain myself from launching into a diatribe about how we should always think thrice before using attr_* because of implications for encapsulation.</div><div><br></div><div>Cheers,</div><div><br></div><div>Other Tom</div></body></html>