[LRUG] A question on DRYness on testing methods that get / set state

Tom Stuart tom at therye.org
Wed Nov 23 00:04:12 PST 2011


On 22 Nov 2011, at 19:41, Tatsuya Ono wrote:

> Other solution is to use instance_variable_get and instance_variable_set methods.
> 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_set
> 
> 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].
> 
> Cheers,
> 
> Tatsuya


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…

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.

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.

Cheers,

Other Tom
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lrug.org/pipermail/chat-lrug.org/attachments/20111123/d90ed3ca/attachment-0003.html>


More information about the Chat mailing list