$LOAD_PATH << File.dirname(__FILE__)
require 'spec_helper'
describe 'A REST adapter' do
before do
@adapter = DataMapper::Repository.adapters[:default]
end
describe 'when getting one resource' do
describe 'if the resource exists' do
before do
book_xml = <<-BOOK
Stephen King
2008-06-08T17:03:07Z
1
The Shining
2008-06-08T17:03:07Z
BOOK
@id = 1
@response = mock(Net::HTTPResponse)
@response.stub!(:body).and_return(book_xml)
@adapter.stub!(:http_get).and_return(@response)
end
it 'should return the resource' do
book = Book.get(@id)
book.should_not be_nil
book.id.should be_an_instance_of(Fixnum)
book.id.should == 1
end
it 'should do an HTTP GET' do
@adapter.should_receive(:http_get).with('/books/1.xml').and_return(@response)
Book.get(@id)
end
it "it be equal to itself" do
Book.get(@id).should == Book.get(@id)
end
end
describe 'if the resource does not exist' do
it 'should return nil' do
@id = 1
@response = mock(Net::HTTPNotFound)
@response.stub!(:content_type).and_return('text/html')
@response.stub!(:body).and_return('')
@adapter.stub!(:http_get).and_return(@response)
id = 4200
Book.get(id).should be_nil
end
end
end
describe 'when getting all resource of a particular type' do
before do
books_xml = <<-BOOK
Ursula K LeGuin
2008-06-08T17:02:28Z
1
The Dispossed
2008-06-08T17:02:28Z
Stephen King
2008-06-08T17:03:07Z
2
The Shining
2008-06-08T17:03:07Z
BOOK
@response = mock(Net::HTTPResponse)
@response.stub!(:body).and_return(books_xml)
@adapter.stub!(:http_get).and_return(@response)
end
it 'should get a non-empty list' do
Book.all.should_not be_empty
end
it 'should receive one Resource for each entity in the XML' do
Book.all.size.should == 2
end
it 'should do an HTTP GET' do
@adapter.should_receive(:http_get).and_return(@response)
Book.first
end
end
end