spec/lib/ninja_model/persistence_spec.rb in ninja-model-0.9.3 vs spec/lib/ninja_model/persistence_spec.rb in ninja-model-0.9.4
- old
+ new
@@ -1,16 +1,16 @@
require 'spec_helper'
describe NinjaModel::Persistence do
+ let(:adapter) { mock('Adapter') }
class PersistenceModel < NinjaModel::Base
attribute :testing, :integer
end
before {
@obj = PersistenceModel.new
- @adapter = mock('Adapter')
- @obj.class.stubs(:adapter).returns(@adapter)
+ @obj.class.stubs(adapter: adapter)
}
subject { @obj }
it { should respond_to(:save) }
it { should respond_to(:create) }
it { should respond_to(:update) }
@@ -23,113 +23,83 @@
its(:new_record?) { should be_true }
its(:persisted?) { should be_false }
its(:destroyed?) { should be_false }
describe 'save' do
- before { subject.stubs(:run_callbacks).yields(true) }
- it 'should run the save callbacks' do
- subject.expects(:run_callbacks).with(:save)
- subject.save
- end
-
it 'should call create for a new record' do
- subject.stubs(:new_record?).returns(true)
+ subject.stubs(new_record?: true)
subject.expects(:create)
subject.save
end
it 'should call update for a persisted record' do
- subject.stubs(:new_record?).returns(false)
+ subject.stubs(new_record?: false)
subject.expects(:update)
subject.save
end
it 'should clear the changed attributes after successful save' do
attributes = {}
- subject.stubs(:changed_attributes).returns(attributes)
- subject.stubs(:new_record?).returns(false)
- subject.stubs(:update).returns(true)
+ subject.stubs(changed_attributes: attributes, new_record?: false, update: true)
attributes.expects(:clear)
subject.save
end
it 'should not clear the changed attributes after unsuccessful save' do
attributes = {}
- subject.stubs(:changed_attributes).returns(attributes)
- subject.stubs(:new_record?).returns(false)
- subject.stubs(:update).returns(false)
+ subject.stubs(changed_attributes: attributes, new_record?: false, update: false)
attributes.expects(:clear).never
subject.save
end
end
describe 'create' do
- before {
- subject.stubs(:run_callbacks).yields(true)
- @adapter.stubs(:create).returns(true)
- }
- it 'should run the create callbacks' do
- subject.expects(:run_callbacks).with(:create)
- subject.create
- end
it 'should call create on the adapter' do
- @adapter.expects(:create).with(subject)
+ adapter.expects(:create).with(subject)
subject.create
end
it 'should update the persisted status' do
+ adapter.stubs(create: true)
subject.create
subject.persisted?.should be_true
end
end
describe 'update' do
- before {
- subject.stubs(:run_callbacks).yields(true)
- @adapter.stubs(:update).returns(true)
- }
- it 'should run the update callbacks' do
- subject.expects(:run_callbacks).with(:update)
- subject.update
- end
it 'should call update on the adapter' do
- @adapter.expects(:update).with(subject)
+ adapter.expects(:update).with(subject)
subject.update
end
end
describe 'destroy' do
- before {
- @adapter.stubs(:destroy).returns(true)
- }
- it 'should run the destroy callbacks' do
- subject.expects(:run_callbacks).with(:destroy)
- subject.destroy
- end
it 'should call destroy on the adapter' do
- @adapter.expects(:destroy).with(subject)
+ adapter.expects(:destroy).with(subject)
subject.destroy
end
it 'should update the destroyed? status' do
+ adapter.stubs(destroy: true)
subject.destroy
subject.destroyed?.should be_true
end
it 'should not be persisted afterwards' do
+ adapter.stubs(destroy: true)
subject.destroy
subject.persisted?.should be_false
end
end
describe 'reload' do
it 'should call reload on the adapter' do
- @adapter.expects(:reload).with(subject)
+ adapter.expects(:reload).with(subject)
subject.reload
end
end
describe 'update_attributes' do
it 'update the attributes hash and save' do
subject.expects(:save)
- subject.update_attributes(:testing => 2)
+ subject.update_attributes(testing: 2)
subject.testing.should eql(2)
end
end
end