require 'spec_helper' describe Fetcher::Microdata do before do class OnTheFly < Fetcher::Microdata; end end it 'should include Discoverer::Writer' do OnTheFly.ancestors.should include Discoverer::Writer end it 'should include Virtus' do OnTheFly.ancestors.should include Virtus end describe '.new' do context 'a social network flag and a social network specific data is passed' do it 'should call the right coercer' do social_network = :facebook user_data = stub 'user data' final_data = stub 'final_data' Fetcher::Microdata.any_instance .should_receive(:coerce) .with(social_network, user_data) .and_return final_data Fetcher::Microdata.any_instance.should_receive(:attributes=) .with(final_data) Fetcher::Microdata.new social_network, user_data end end context 'the person is initialized with a hash' do it 'should accept the hash as an argument and pass it to attributes' do attribute_hash = {} Fetcher::Microdata.any_instance.should_receive(:attributes=) .with attribute_hash Fetcher::Microdata.new attribute_hash end end end describe '#coerce' do context 'the right coercer exists for a subclass' do it 'should return call the right coercer with the data and return the result' do module Fetcher; class Microdata; class SubClass < Fetcher::Microdata; module Facebook; Coercer = proc {|message| }; end; end; end; end raw_data = stub 'data' final_data = stub 'final_data' Fetcher::Microdata::SubClass::Facebook::Coercer.should_receive(:call) .with(raw_data) .and_return final_data Fetcher::Microdata::SubClass.any_instance.should_receive(:attributes=).with final_data Fetcher::Microdata::SubClass.new :facebook, raw_data end end context 'there is no such coercer' do it 'should raise a relevant error' do raw_data = stub 'data' final_data = stub 'final_data' Fetcher::Microdata.any_instance.stub(:attributes=) expect { Fetcher::Microdata.new :strange, raw_data }.to raise_error Fetcher::Microdata::UnknownSocialNetworkError, "Couldn't find coertion algorithm for 'strange' social network, please require 'fetcher-microdata-strange' in your project and make sure it has a Fetcher::Microdata::Strange::Coercer" end end context 'there was a name error but not implying the social network' do it 'should let it rise' do module Fetcher; class Microdata; module Red; Coercer = proc {}; end; end; end raw_data = stub 'data' Fetcher::Microdata::Red::Coercer.should_receive(:call).and_raise NameError.new 'no description' expect { Fetcher::Microdata.new :red, raw_data }.to raise_error NameError, 'no description' end end end end