require 'spec_helper' describe MusicBrainz do describe '.cache_contents', :vcr do let(:cache_path) { File.join(File.dirname(__FILE__), '../../tmp/rspec_test/musicbrainz') } let(:response) { ::StringIO.new('KasabianKasabianGB1999http://allmusic.com/artist/p678134http://en.wikipedia.org/wiki/Kasabianhttp://twitter.com/kasabianhqhttp://www.bbc.co.uk/music/artists/69b39eab-6577-46a4-a9f5-817839092033http://www.discogs.com/artist/Kasabianhttp://www.facebook.com/kasabianhttp://www.imdb.com/name/nm1868442/http://www.kasabian.co.uk/http://www.myspace.com/kasabianhttp://www.youtube.com/kasabianvevohttp://www.youtube.com/user/KasabianTour') } before(:each) do MusicBrainz.clear_cache end after(:each) do MusicBrainz.clear_cache end context 'with cache enabled' do it 'calls get contents only once when requesting the resource twice' do MusicBrainz.cache_path = cache_path MusicBrainz.stub(:get_contents).and_return(response) MusicBrainz.should_receive(:get_contents).once mbid = '69b39eab-6577-46a4-a9f5-817839092033' File.exist?("#{cache_path}/artist/#{mbid}?inc=url-rels").should be_false artist = MusicBrainz::Artist.find(mbid) artist.should be_a_kind_of(MusicBrainz::Artist) File.exist?("#{cache_path}/artist/#{mbid}?inc=url-rels").should be_true artist = MusicBrainz::Artist.find(mbid) artist.should be_a_kind_of(MusicBrainz::Artist) end end context 'with cache disabled' do it 'calls get contents twice when requesting the resource twice' do MusicBrainz.cache_path = nil MusicBrainz.stub(:get_contents).and_return(response) MusicBrainz.should_receive(:get_contents).twice mbid = '69b39eab-6577-46a4-a9f5-817839092033' File.exist?("#{cache_path}/artist/#{mbid}?inc=url-rels").should be_false artist = MusicBrainz::Artist.find(mbid) artist.should be_a_kind_of(MusicBrainz::Artist) File.exist?("#{cache_path}/artist/#{mbid}?inc=url-rels").should be_false response.rewind MusicBrainz.stub(:get_contents).and_return(response) artist = MusicBrainz::Artist.find(mbid) artist.should be_a_kind_of(MusicBrainz::Artist) end end end end