Created
August 20, 2014 11:22
-
-
Save marcinb/23935b472aa5720abf5b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# To run this code: | |
# | |
# gem install rspec | |
# rspec code.rb | |
# | |
require 'rspec' | |
User = Struct.new(:github_handle, :followers) | |
class Github # External dependency | |
def self.fetch_followers(*) | |
%w(alex marcin marek) | |
end | |
end | |
# Client code | |
class SomeClientCode | |
def update_followers_for(user) | |
gh = GithubFollowers.new(user) | |
unless gh.any? | |
gh.fetch! | |
gh.update | |
end | |
end | |
end | |
class GithubFollowers | |
@@followers = [] | |
attr_reader :user | |
def self.followers | |
@@followers | |
end | |
def initialize(user) | |
@user = user | |
end | |
def fetch! | |
@@followers = Github.fetch_followers(user.github_handle) | |
end | |
def any? | |
@@followers.any? | |
end | |
def update | |
followers = [] | |
user.followers = [] | |
@@followers.each do |follower| | |
user.followers << follower | |
followers << follower | |
end | |
followers | |
end | |
end | |
RSpec.describe GithubFollowers do | |
let(:user) { User.new('hans', []) } | |
let(:subject) { described_class.new(user) } | |
it 'fetches the github followers' do | |
allow(Github).to receive(:fetch_followers) { %w(john james finn) } | |
subject.fetch! | |
expect(subject.update).to eq(%W(john james finn)) | |
expect(user.followers).to eq(%w(john james finn)) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment