spec/callbacks_spec.rb in asset_cloud-2.2.8 vs spec/callbacks_spec.rb in asset_cloud-2.2.9

- old
+ new

@@ -1,18 +1,23 @@ require 'spec_helper' class CallbackAsset < AssetCloud::Asset before_store :callback_before_store + before_delete :callback_before_delete after_delete :callback_after_delete before_validate :make_value_valid after_validate :add_spice validate :valid_value private + + def callback_before_delete(*args); end + def make_value_valid self.value = 'valid' end + def add_spice self.value += ' spice' end def valid_value @@ -30,10 +35,13 @@ after_delete :callback_after_delete before_delete :callback_before_delete after_write :callback_after_write before_write :callback_before_write + + def callback_before_write(*args); end + def callback_after_write(*args); end end class MethodRecordingCloud < AssetCloud::Base attr_accessor :run_callbacks @@ -46,35 +54,48 @@ @run_callbacks << method.to_sym end end describe CallbackCloud do - before { @fs = CallbackCloud.new(File.dirname(__FILE__) + '/files', 'http://assets/') } + before do + @fs = CallbackCloud.new(File.dirname(__FILE__) + '/files', 'http://assets/') + @fs.write('tmp/file.txt', 'foo') + end it "should invoke callbacks after store" do @fs.should_receive(:callback_before_write).with('tmp/file.txt', 'text').and_return(true) @fs.should_receive(:callback_after_write).with('tmp/file.txt', 'text').and_return(true) - @fs.write 'tmp/file.txt', 'text' + @fs.write('tmp/file.txt', 'text').should == true + @fs.read('tmp/file.txt').should == 'text' end it "should invoke callbacks after delete" do @fs.should_receive(:callback_before_delete).with('tmp/file.txt').and_return(true) @fs.should_receive(:callback_after_delete).with('tmp/file.txt').and_return(true) - @fs.delete 'tmp/file.txt' + @fs.delete('tmp/file.txt').should == 'foo' end + it "should not invoke other callbacks when a before_ filter returns false" do + @fs.should_receive(:callback_before_delete) + .with('tmp/file.txt') + .and_return(false) + @fs.should_not_receive(:callback_after_delete) + + @fs.delete('tmp/file.txt').should == nil + end + it "should invoke callbacks even when constructing a new asset" do @fs.should_receive(:callback_before_write).with('tmp/file.txt', 'hello').and_return(true) @fs.should_receive(:callback_after_write).with('tmp/file.txt', 'hello').and_return(true) asset = @fs.build('tmp/file.txt') asset.value = 'hello' - asset.store + asset.store.should == true end end describe MethodRecordingCloud do before do @@ -111,8 +132,15 @@ it "should run its after_delete callback after delete is called" do @asset.should_not_receive(:callback_before_store) @asset.should_receive(:callback_after_delete).and_return(true) - @asset.delete + @asset.delete.should == 'bar' + end + + it "not invoke other callbacks when a before_ filter returns false" do + @asset.should_receive(:callback_before_delete).and_return(false) + @asset.should_not_receive(:callback_after_delete) + + @asset.delete.should == nil end end