Skip to content

Instantly share code, notes, and snippets.

@aCandidMind
Forked from anonymous/gist:5101448
Created September 11, 2017 13:18
Show Gist options
  • Save aCandidMind/8772d1278620f5a88f34158dfdfea7a4 to your computer and use it in GitHub Desktop.
Save aCandidMind/8772d1278620f5a88f34158dfdfea7a4 to your computer and use it in GitHub Desktop.
Coupling - mock,stub
class RequestsSchedule
def fetch_all_expired
# fetch and return all expired definitions from MongoDB
# e.g. use MongoClient somehow
end
end
class RequestToQueuePusher
QUEUE_NAME = 'requests'
def enqueue(requests)
each.requests { |r| enqueue_single(r) }
end
private
def enqueue_singe(request)
#locate messages queue
# push request to queue
end
end
class SchedulerJob
def run
expired_requests = RequestSchedule.new.fetch_all_expired
RequestToQueuePusher.new.enqueue(expired_requests)
end
end
@aCandidMind
Copy link
Author

Below is a StackOverflow comment where this gist was linked from.

Take a look at this example I created gist.github.com/anonymous/5101448. If I want to test SchedulerJob I need to stub RequestSchedule and mock RequestToQueuePusher right? Also what bothers me is that SchedulerJob is tightly coupled with two remaining classes. As I come from Java world, I'd normally extract them as dependencies as there is no easy way to fake objects which are created the hard-coded way. In Ruby it seems not to be an issue. I see a lot of object like my SchedulerJob. I know there is a way to fake them easily, but for me it violates some SOLID principles – grafthez Mar 6 '13 at 18:06

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment