Notes posted by aghyad
RSS feed
correct, but ..
stub_chain provides a very good replacement of long lines of nested stubs, but never forget it violates Law of Demeter; i.e. it indicates an increase of coupling in your classes and this is a bad thing because it means your objects now are making more unnecessary calls to other objects. for example:
def initialize(some_obj) @obj = some_obj end def foo @obj.x # GOOD coupling - according to LoD you are allowed to call a method on your object end def bar @obj.x.y # BAD coupling - can not call a method on a returned value of another method call even if the initial call is legal end
How is this related to TDD and stubs?
-
method foo test will have only one stub for a double of some_obj type
-
method bar will have 2 stubs: the first is going to swallow the other one to produce the result (and then can be shortened using this stub_chain technique)
Always remember: if your tests are using stub_chains –> your code is smelly and possibly tightly coupled.