spec/base_spec.rb in reviewed-0.0.6 vs spec/base_spec.rb in reviewed-0.0.7

- old
+ new

@@ -1,97 +1,60 @@ require 'spec_helper' -module Reviewed - class Example < Base - end -end +describe Reviewed::Base do -module Reviewed - class Article < Base - end -end + let(:article_id) { '509d166d60de7db97c05ce71' } -describe Reviewed::Base do - before(:each) do - Reviewed.api_key = TEST_KEY - end + class Example < Reviewed::Base; end describe 'Class' do + it 'responds to model_name' do - Reviewed::Example.model_name.should eql("Reviewed::Example") + Example.model_name.should eql("Example") end end - describe 'initialization' do - it 'raises an error if a resource ID is not present' do - expect { - Reviewed::Example.new - }.to raise_error(Reviewed::ResourceError) + describe 'initialize' do + + it 'objectifies data' do + Example.any_instance.should_receive(:objectify).with({}) + obj = Example.new( {} ) end - it 'returns a value' do - model = Reviewed::Example.new(id: 1234, name: 'Test') - model.id.should == 1234 + it 'sets the data in the @attributes var' do + obj = Example.new( { foo: 'bar' } ) + obj.instance_variable_get(:@attributes).should eql( { foo: 'bar' } ) end end describe 'find' do - before(:each) do - Reviewed::Example.resource_name = 'article' # test - end + use_vcr_cassette 'base/article/find' - it 'raises an error if the api key is not set' do - Reviewed.api_key = nil - - expect { - Reviewed::Example.find('test') - }.to raise_error(Reviewed::ConfigurationError) + it 'returns an article' do + Reviewed::Article.find("#{article_id}").should be_an_instance_of(Reviewed::Article) end - context 'with a valid request' do - use_vcr_cassette 'base/find_ok' - - before(:each) do - @article = Reviewed::Article.find('big-green-egg-medium-charcoal-grill-review') - end - - it 'fetches content from the api' do - model = Reviewed::Example.find(@article.id) - model.raw_response.should_not be_nil - end - - it 'parses response json and returns an object' do - model = Reviewed::Example.find(@article.id) - model.class.should == Reviewed::Example - model.name.should == 'Big Green Egg Medium Charcoal Grill Review' - end + it 'calls object_from_response with correct params' do + Reviewed::Article.should_receive(:object_from_response).with( + :get, "articles/#{article_id}", {} + ) + Reviewed::Article.find("#{article_id}") end + end - context 'with an invalid request' do - use_vcr_cassette 'base/find_error_key' + describe 'resource_url' do - it 'complains if the api key is unauthorized' do - Reviewed.api_key = 'xxxxxxxxxxxxxxxx' - - expect { - Reviewed::Example.find('50241b9c5da4ac8d38000001') - }.to raise_error(Reviewed::ResourceError, /unauthorized/i) - end - - it 'complains if the requested resource is not found' do - expect { - Reviewed::Example.find('notfound') - }.to raise_error(Reviewed::ResourceError, /not found/i) - end + it 'should the demodulized resource name' do + Reviewed::Article.resource_url.should eql("articles") end end describe 'where' do use_vcr_cassette 'base/where_collection' it 'returns a collection' do - collection = Reviewed::Article.all + collection = Reviewed::Article.where collection.class.should == Reviewed::Collection end it 'returns the appropriate page of results' do collection = Reviewed::Article.where(:page => 2) @@ -111,26 +74,28 @@ collection.total.should == 0 collection.out_of_bounds.should be_true end end + describe 'all' do + + it 'calls where with empty params' do + Reviewed::Article.should_receive(:where).with({}) + Reviewed::Article.all + end + end + describe 'attributes' do it 'returns the named attribute (via method missing)' do - model = Reviewed::Example.new(:id => 'id', :super_awesome => 'true') + model = Example.new(:id => 'id', :super_awesome => 'true') model.super_awesome.should == 'true' end end - describe 'resource url' do - it 'returns an individual resource' do - Reviewed::Example.resource_url('article-123').should == 'https://the-guide-staging.herokuapp.com/api/v1/articles/article-123' - end + describe 'resource_url' do - it 'returns a collection of resources' do - Reviewed::Example.resource_url(nil).should == 'https://the-guide-staging.herokuapp.com/api/v1/articles' - end - - it 'includes a query parameter' do - Reviewed::Example.resource_url(nil, page: 2).should == 'https://the-guide-staging.herokuapp.com/api/v1/articles?page=2' + it 'returns the pluralized demodulized class name' do + Reviewed::Article.resource_url.should eql('articles') + Example.resource_url.should eql('examples') end end end