Skip to content

Instantly share code, notes, and snippets.

@dodecaphonic
Last active December 14, 2015 01:29
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 dodecaphonic/5007016 to your computer and use it in GitHub Desktop.
Save dodecaphonic/5007016 to your computer and use it in GitHub Desktop.
class MiopicAPI
def do_something_with(foo_id, bar_id)
foo = Foo.find(foo_id)
bar = Bar.find(bar_id)
# ...
end
end
# Refactoring 1
class MiopicAPI
# Aqui você pode injetar seu parâmetro em teste numa boa, porque
# o bloco em #fetch só será executado se stores não contiver a chave
# especificada. Mesmo que a constante não esteja definida não haverá
# problema -- é lazily evaluated.
def do_something_with(foo_id, bar_id, stores = {})
foo_store = stores.fetch(:foo) { Foo }
bar_store = stores.fetch(:bar) { Bar }
foo = foo_store.find(foo_id)
bar = bar_store.find(bar_id)
# ...
end
end
# Refactoring 2
# Como Foo e Bar só seriam verificados se os métodos executassem, você
# pode fazer algo como
#
# miopic_api_instance.should_receive(:foo_store).and_return double('foo')
#
# sem nenhum problema, mais uma vez sem afetar o código em produção.
class MiopicAPI
def do_something_with(foo_id, bar_id)
foo = foo_store.find(foo_id)
bar = bar_store.find(bar_id)
# ...
end
def foo_store
Foo
end
def bar_store
Bar
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment