Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# 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