From 9498e71319c8d8383db59f31638c72cdef9efc32 Mon Sep 17 00:00:00 2001 From: Austin Schneider Date: Tue, 17 Apr 2012 11:24:04 -0400 Subject: [PATCH] tests for Pagination --- spec/topspin_api/pagination_spec.rb | 48 +++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 spec/topspin_api/pagination_spec.rb diff --git a/spec/topspin_api/pagination_spec.rb b/spec/topspin_api/pagination_spec.rb new file mode 100644 index 0000000..2d584c8 --- /dev/null +++ b/spec/topspin_api/pagination_spec.rb @@ -0,0 +1,48 @@ +require 'spec_helper' + +describe TopspinApi::Pagination do + + before :all do + @klass = Class.new.send :include, described_class + end + + subject { @klass.new } + + describe "decorate" do + context "given a hash-like object with pagination-related keys" do + before do + @hash = {'total_pages' => 52, 'current_page' => 14, 'total_entries' => 362, 'per_page' => 7} + @obj = Object.new + end + + it "adds these keys as methods to the given object" do + subject.decorate @hash, @obj + @obj.total_pages.should == 52 + @obj.current_page.should == 14 + @obj.total_entries.should == 362 + @obj.per_page.should == 7 + end + + it "returns the object" do + subject.decorate(@hash, @obj).should == @obj + end + + it "converts the hash values to integers as needed" do + @hash['per_page'] = '7' + subject.decorate(@hash, @object).per_page.should == 7 + end + + it "gracefully handles missing keys" do + @hash.delete 'total_pages' + expect { subject.decorate(@hash, @obj) }.to_not raise_error + end + end + + context "without a hash-like object" do + it "raises NoMethodError" do + expect { subject.decorate Object.new, Object.new }.to raise_error(NoMethodError) + end + end + end + +end \ No newline at end of file