Skip to content

Instantly share code, notes, and snippets.

@marcinb
Created August 20, 2014 11:22
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 marcinb/23935b472aa5720abf5b to your computer and use it in GitHub Desktop.
Save marcinb/23935b472aa5720abf5b to your computer and use it in GitHub Desktop.
# 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