spec/relation_spec.rb in bootic_client-0.0.15 vs spec/relation_spec.rb in bootic_client-0.0.16

- old
+ new

@@ -1,26 +1,34 @@ require 'spec_helper' describe BooticClient::Relation do let(:client) { double(:client) } - let(:relation) { BooticClient::Relation.new({'href' => '/foo/bars', 'type' => 'application/json', 'title' => 'A relation', 'name' => 'self'}, client) } + let(:attributes) {{'href' => '/foo/bars', 'type' => 'application/json', 'title' => 'A relation', 'name' => 'self'}} + let(:relation) { BooticClient::Relation.new(attributes, client) } describe 'attributes' do it 'has readers for known relation attributes' do expect(relation.href).to eql('/foo/bars') expect(relation.type).to eql('application/json') expect(relation.title).to eql('A relation') expect(relation.name).to eql('self') end end + describe '#to_hash' do + it 'returns attributes' do + hash = relation.to_hash + expect(hash).to eq attributes + end + end + describe '#run' do let(:entity) { BooticClient::Entity.new({'title' => 'Foobar'}, client) } describe 'running GET by default' do it 'fetches data and returns entity' do - client.stub(:request_and_wrap).with(:get, '/foo/bars', BooticClient::Entity, {}).and_return entity + allow(client).to receive(:request_and_wrap).with(:get, '/foo/bars', BooticClient::Entity, {}).and_return entity expect(relation.run).to eql(entity) end context 'without URI templates' do let(:relation) { BooticClient::Relation.new({'href' => '/foos/bar', 'type' => 'application/json', 'title' => 'A relation'}, client) } @@ -65,24 +73,24 @@ describe 'POST' do let(:relation) { BooticClient::Relation.new({'href' => '/foo/bars', 'type' => 'application/json', 'name' => 'self', 'method' => 'post'}, client) } let(:relation_templated) { BooticClient::Relation.new({'href' => '/foo/{bars}', 'templated' => true, 'type' => 'application/json', 'name' => 'self', 'method' => 'post'}, client) } it 'POSTS data and returns resulting entity' do - client.stub(:request_and_wrap).with(:post, '/foo/bars', BooticClient::Entity, {}).and_return entity + allow(client).to receive(:request_and_wrap).with(:post, '/foo/bars', BooticClient::Entity, {}).and_return entity expect(relation.run).to eql(entity) end it 'interpolates templated URLs' do - client.stub(:request_and_wrap).with(:post, '/foo/123', BooticClient::Entity, {foo: 'bar'}).and_return entity + allow(client).to receive(:request_and_wrap).with(:post, '/foo/123', BooticClient::Entity, {foo: 'bar'}).and_return entity expect(relation_templated.run(bars: 123, foo: 'bar')).to eql(entity) end end describe 'DELETE' do let(:relation) { BooticClient::Relation.new({'href' => '/foo/bars', 'type' => 'application/json', 'name' => 'self', 'method' => 'delete'}, client) } it 'DELETEs data and returns resulting entity' do - client.stub(:request_and_wrap).with(:delete, '/foo/bars', BooticClient::Entity, {}).and_return entity + allow(client).to receive(:request_and_wrap).with(:delete, '/foo/bars', BooticClient::Entity, {}).and_return entity expect(relation.run).to eql(entity) end end end end