spec/unit/resources_base_spec.rb in syncano-4.0.0.alpha1 vs spec/unit/resources_base_spec.rb in syncano-4.0.0.alpha2

- old
+ new

@@ -72,11 +72,11 @@ { 'type' => 'list', 'name' => 'triggers' }, { 'type' => 'list', 'name' => 'webhooks' }] }, :collection => { :path => '/v1/instances/', - :http_methods => ['post', + :http_methods => ['post', 'get'], :params => [] }, :member => { :path => '/v1/instances/{ name }/', :http_methods => ['put', 'get', @@ -138,11 +138,11 @@ it 'should instantiate new resource' do expect(subject.new(connection, {}, {}, true)).to be_a subject end it 'should init attributes' do - resource = subject.new(connection, {}, { name: 'test' }, true) + resource = subject.new(connection, {}, { name: 'test' }) expect(resource.name).to eq('test') end it 'should clean changes if initialized from database' do resource = subject.new(connection, {}, { links: { self: '/v1/instances/test/' }, name: 'test' }, true) @@ -151,35 +151,96 @@ it 'should keep changes if not initialized from database' do resource = subject.new(connection, {}, { links: { self: '/v1/instances/test/' }, name: 'test' }, false) expect(resource.changed?).to eq(true) end + end - it 'should mark resource as not new if initialized with self path' do - resource = subject.new(connection, {}, { links: { self: '/v1/instances/test/' } }, true) - expect(resource.new_record?).to be(false) + describe '#new_record?' do + let(:resource) { subject.new connection, {}, { name: 'asd' }, from_db } + + context 'is true' do + let(:from_db) { false } + + specify { expect(resource.new_record?).to eq(true) } end - it 'should mark resource as new if initialized without self path' do - resource = subject.new(connection, {}, { name: 'test' }, true) - expect(resource.new_record?).to be(true) + context 'is false' do + let(:from_db) { true } + + specify { expect(resource.new_record?).to eq(false) } end end - describe '.create' do - it 'should create new object in Syncano' do + describe "persisting data" do + shared_context "resource invalid" do + before { expect(resource).to receive(:valid?) { false } } + end + shared_context "resource valid" do + before do + # expect(instance_of(Syncano::Resource::Base)).to receive(:valid?) { true } + + expect(resource).to receive(:valid?) { true } + + expect(connection).to receive(:request). + with(instance_of(Symbol), instance_of(String), instance_of(Hash)). + and_return({}) + end end - end - describe '.update_attributes' do - it 'should create update object\'s attributes in Syncano' do + let(:resource) { subject.new connection, {}, {}, false } + describe ".create!" do + before do + expect(subject).to receive(:new).and_return(resource) + expect(resource).to receive(:save!).and_return(resource) + end + + specify { expect(subject.create!(connection, {}, {})).to eq(resource) } end - end - describe '.destroy' do - it 'should delete object from Syncano' do + describe ".create" do + before do + expect(subject).to receive(:new).and_return(resource) + expect(resource).to receive(:save).and_return(false) + end + specify { expect(subject.create(connection, {}, {})).to eq(resource) } + specify { expect(subject.create(connection, {}, {}).new_record?).to eq(true) } + end + + describe "#save" do + context "when invalid" do + include_context "resource invalid" + + specify { expect(resource.save).to eq(false) } + end + + context "when valid" do + include_context "resource valid" + + specify { expect(resource.save).to be_kind_of(Syncano::Resources::Base) } + end + end + + describe "#save!" do + context "when invalid" do + include_context "resource invalid" + + specify do + expect { + resource.save! + }.to raise_error(Syncano::Resources::ResourceInvalid) + end + end + + context "when valid" do + include_context "resource valid" + + specify do + expect(resource.save!).to be_kind_of(Syncano::Resources::Base) + end + end end end end \ No newline at end of file