Don't rely on external resources (gravatar) in integration tests

element
Marcin Kulik 9 years ago
parent 2857ba303e
commit 939137cff6

@ -1,16 +1,22 @@
module AvatarHelper
def avatar_image_tag
h.image_tag avatar_url, alt: (model.username || model.temporary_username), class: 'avatar'
h.image_tag avatar_url(model), alt: (model.username || model.temporary_username), class: 'avatar'
end
private
module GravatarURL
def avatar_url(model)
username = model.username || model.temporary_username
email = model.email || "#{username}+#{model.id}@asciinema.org"
hash = Digest::MD5.hexdigest(email.downcase)
"//gravatar.com/avatar/#{hash}?s=128&d=retro"
end
end
def avatar_url
username = model.username || model.temporary_username
email = model.email || "#{username}+#{model.id}@asciinema.org"
hash = Digest::MD5.hexdigest(email.downcase)
"//gravatar.com/avatar/#{hash}?s=128&d=retro"
module TestAvatarURL
def avatar_url(model)
h.image_path 'favicon.png'
end
end
end

@ -35,3 +35,5 @@ Rails.application.configure do
# Raises error for missing translations
# config.action_view.raise_on_missing_translations = true
end
AvatarHelper.include(AvatarHelper::GravatarURL)

@ -76,3 +76,5 @@ Rails.application.configure do
# Do not dump schema after migrations.
config.active_record.dump_schema_after_migration = false
end
AvatarHelper.include(AvatarHelper::GravatarURL)

@ -37,3 +37,5 @@ Rails.application.configure do
# Raises error for missing translations
# config.action_view.raise_on_missing_translations = true
end
AvatarHelper.include(AvatarHelper::TestAvatarURL)

@ -1,30 +1,23 @@
require 'rails_helper'
describe AvatarHelper do
describe AvatarHelper::GravatarURL do
def expected_img(src)
%(<img alt="satyr" class="avatar" src="#{src.gsub('&', '&amp;')}" />)
end
let(:decorator) { double('decorator', h: h, model: model).
extend(described_class) }
let(:decorator) { double('decorator').extend(described_class) }
let(:model) { double('model', id: 1, username: 'satyr', email: email) }
describe '#avatar_image_tag' do
subject { decorator.avatar_image_tag }
describe '#avatar_url' do
subject { decorator.avatar_url(model) }
context "when user has an email" do
let(:email) { 'foo@email.com' }
it { should eq(expected_img(
'//gravatar.com/avatar/9dcfeb70fe212ea12562dddd22b0fc92?s=128&d=retro')) }
it { should eq('//gravatar.com/avatar/9dcfeb70fe212ea12562dddd22b0fc92?s=128&d=retro') }
end
context "when user has no email" do
let(:email) { nil }
it { should eq(expected_img(
'//gravatar.com/avatar/40affe80f7becd02ac38d316f7fe7057?s=128&d=retro')) }
it { should eq('//gravatar.com/avatar/40affe80f7becd02ac38d316f7fe7057?s=128&d=retro') }
end
end

Loading…
Cancel
Save