Skip to content

Instantly share code, notes, and snippets.

@ralphos
Created January 5, 2013 01:19
Show Gist options
  • Save ralphos/4459043 to your computer and use it in GitHub Desktop.
Save ralphos/4459043 to your computer and use it in GitHub Desktop.
Here's my implementation using a 'fake' to simulate a call to the Instagram API. It's very similar to the Hashtag app example, with the exception of the FakeInstagram class where I didn't create a '.[]=' method since I'm not really searching for different terms like you would using Twitter. The actual Instagram response uses Hashie::Mash which I…
class FakeInstagram
def self.client(opts = {})
instagram_response = Hashie::Mash.new
instagram_response.images!.thumbnail!.url = "someawesomeimage.jpg"
OpenStruct.new(user_recent_media: [instagram_response])
end
end
require 'spec_helper'
describe FakeInstagram, '.client' do
it 'returns an object that responds to recent media query just like Instagram' do
user = create(:user)
fake = FakeInstagram.client(user.access_token)
expect(fake.user_recent_media).to be_an Array
expect(fake.user_recent_media.length).to eq 1
expect(fake.user_recent_media.first).to be_a Hashie::Mash
end
end
class PhotoSearcher
class_attribute :photo_service
self.photo_service = Instagram
attr_accessor :media
def initialize(user)
@media = photo_service.client(access_token: user.access_token).user_recent_media
end
def thumbnail_urls
@media.map do |photo|
photo.images.thumbnail.url
end
end
end
require 'spec_helper'
describe PhotoSearcher, '#media' do
it 'returns latest photos from Instagram' do
user = create(:user)
photo_searcher = PhotoSearcher.new(user)
photos = photo_searcher.thumbnail_urls
expect(photos.length).to be 20
end
end
describe PhotoSearcher, 'using a custom photo service' do
it 'returns results from the photo service passed in when querying for recent media' do
media_results = stub('user recent media', user_recent_media: [1,2,3])
fake_photo_service = stub('fake photo service', client: media_results)
PhotoSearcher.photo_service = fake_photo_service
user = create(:user)
photo_service = PhotoSearcher.new(user)
expect(photo_service.media).to eq media_results.user_recent_media
end
end
require 'spec_helper'
feature 'User adds a product' do
# ...
scenario 'with photos taken from Instagram' do
user_signs_in_and_clicks_sell
user_should_see_image('distilleryimage', 20)
end
scenario 'with photos from a custom photo service' do
PhotoSearcher.photo_service = FakeInstagram
user_signs_in_and_clicks_sell
user_should_see_image('someawesomeimage.jpg', 1)
end
def user_signs_in_and_clicks_sell
user = create(:user)
sign_in_with_omniauth
click_link 'Sell'
end
def user_should_see_image(url_text, count)
expect(page).to have_css 'li.photo', count: count
photo_url = find('input#photo_0').value
expect(photo_url).to match /#{url_text}/
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment