spec/unit/has_attachment_spec.rb in bulldog-0.1.1 vs spec/unit/has_attachment_spec.rb in bulldog-0.2.0
- old
+ new
@@ -11,13 +11,12 @@
end
it "should provide a query method for the attachment" do
Thing.has_attachment :photo
thing = Thing.new
- file = uploaded_file
thing.photo?.should be_false
- thing.photo = file
+ thing.photo = uploaded_file('test.jpg')
thing.photo?.should be_true
end
it "should configure the existing attachment declared if one exists" do
Thing.has_attachment :photo do
@@ -69,11 +68,11 @@
calls = []
Thing.has_attachment :photo do
style :normal
process(:on => :my_event){calls << 1}
end
- thing = Thing.new(:photo => uploaded_file)
+ thing = Thing.new(:photo => uploaded_file('test.jpg'))
thing.process_attachment(:photo, :my_event)
calls.should == [1]
end
end
@@ -106,11 +105,11 @@
context = nil
Thing.has_attachment :photo do
style :normal
process(:on => :my_event, :with => :test){context = self}
end
- thing = Thing.new(:photo => uploaded_file)
+ thing = Thing.new(:photo => uploaded_file('test.jpg'))
thing.process_attachment(:photo, :my_event)
context.should be_a(Processor::Test)
end
end
@@ -118,17 +117,21 @@
context = nil
Thing.has_attachment :photo do
style :normal
process(:on => :my_event){context = self}
end
- thing = Thing.new(:photo => uploaded_file)
+ thing = Thing.new(:photo => uploaded_file('test.jpg'))
thing.process_attachment(:photo, :my_event)
context.should be_a(Processor::Base)
end
end
describe "object lifecycle" do
+ def test_image_file
+ uploaded_file('test.jpg')
+ end
+
outline "building a record" do
with_model_class :Thing do
spec = self
Thing.has_attachment :attachment do
@@ -198,11 +201,11 @@
describe "when no attributes are stored" do
use_model_class(:Thing)
before do
- @file = uploaded_file('test.jpg', 'test.jpg')
+ @file = uploaded_file('test.jpg')
end
def configure(&block)
Thing.attachment_reflections[:photo].configure(&block)
end
@@ -293,20 +296,20 @@
end
describe "when the record already exists" do
describe "when a file name is set, and the original file exists" do
def instantiate
- file = uploaded_file('test.jpg', 'test.jpg')
+ file = uploaded_file('test.jpg')
thing = Thing.create(:photo => file)
@thing = Thing.find(thing.id)
end
it "should have stored attributes set" do
instantiate
@thing.photo_file_name.should == 'test.jpg'
@thing.photo_content_type.split(/;/).first.should == "image/jpeg"
- @thing.photo_file_size.should == File.size(test_path('test.jpg'))
+ @thing.photo_file_size.should == File.size("#{ROOT}/spec/data/test.jpg")
end
end
describe "when the no file name is set, and the original file does not exist" do
before do
@@ -321,21 +324,21 @@
end
end
describe "when a file name is set, but the original file is missing" do
def instantiate
- file = uploaded_file('test.jpg', 'test.jpg')
+ file = uploaded_file('test.jpg')
@thing = Thing.create(:photo => file)
File.unlink(original_path)
@thing = Thing.find(@thing.id)
end
it "should have stored attributes set" do
instantiate
@thing.photo_file_name.should == 'test.jpg'
@thing.photo_content_type == "image/jpeg"
- @thing.photo_file_size.should == File.size(test_path('test.jpg'))
+ @thing.photo_file_size.should == File.size("#{ROOT}/spec/data/test.jpg")
end
describe "when the record is saved" do
before do
instantiate
@@ -355,18 +358,18 @@
@thing = Thing.find(thing.id)
end
describe "when an attachment is assigned" do
before do
- @file = uploaded_file('test.jpg', 'test.jpg')
+ @file = uploaded_file('test.jpg')
end
it "should set the stored attributes" do
@thing.photo = @file
@thing.photo_file_name.should == 'test.jpg'
@thing.photo_content_type.split(/;/).first.should == "image/jpeg"
- @thing.photo_file_size.should == File.size(test_path('test.jpg'))
+ @thing.photo_file_size.should == File.size("#{ROOT}/spec/data/test.jpg")
end
it "should not create the original file" do
lambda do
@thing.photo = @file
@@ -387,11 +390,11 @@
end
end
describe "when the record exists and there is an attachment" do
before do
- @old_file = test_file('test.jpg')
+ @old_file = uploaded_file('test.jpg')
thing = Thing.create(:photo => @old_file)
@thing = Thing.find(thing.id)
end
def old_original_path
@@ -404,18 +407,18 @@
File.join(new_dirname, new_basename)
end
describe "when a new attachment is assigned" do
before do
- @new_file = test_file('test.png')
+ @new_file = uploaded_file('test.png')
end
it "should set the stored attributes" do
@thing.photo = @new_file
@thing.photo_file_name.should == 'test.png'
@thing.photo_content_type.split(/;/).first.should == 'image/png'
- @thing.photo_file_size.should == File.size(test_path('test.png'))
+ @thing.photo_file_size.should == File.size("#{ROOT}/spec/data/test.png")
end
it "should not create the new original file yet" do
lambda do
@thing.photo = @new_file
@@ -497,11 +500,11 @@
end
describe "#destroy" do
describe "when the record is new" do
before do
- file = uploaded_file('test.jpg', 'test.jpg')
+ file = uploaded_file('test.jpg')
@thing = Thing.new(:photo => file)
end
it "should not raise an error" do
lambda{@thing.destroy}.should_not raise_error
@@ -524,11 +527,11 @@
end
end
describe "when the record existed and had an attachment" do
before do
- file = uploaded_file('test.jpg', 'test.jpg')
+ file = uploaded_file('test.jpg')
thing = Thing.create(:photo => file)
@thing = Thing.find(thing.id)
end
it "should not stop the record being destroyed" do
@@ -565,11 +568,11 @@
before do
spec = self
Thing.has_attachment :photo do
path "#{spec.temporary_directory}/:id.jpg"
end
- thing = Thing.create(:name => 'old', :photo => uploaded_file)
+ thing = Thing.create(:name => 'old', :photo => uploaded_file('test.jpg'))
@thing = Thing.find(thing.id)
end
def original_path
"#{temporary_directory}/#{@thing.id}.jpg"
@@ -584,11 +587,11 @@
@thing.photo = @thing.photo.value
@thing.photo_changed?.should be_false
end
it "should return true if a new value has been assigned to the attachment" do
- @thing.photo = uploaded_file
+ @thing.photo = uploaded_file('test.jpg')
@thing.photo_changed?.should be_true
end
end
describe "#ATTACHMENT_was" do
@@ -597,31 +600,31 @@
@thing.photo_was.should equal(original_photo)
end
it "should return a clone of the original value after assignment" do
original_photo = @thing.photo
- @thing.photo = uploaded_file
+ @thing.photo = uploaded_file('test.jpg')
@thing.photo_was.should_not equal(original_photo)
@thing.photo_was.should == original_photo
end
end
describe "#changes" do
it "should return attachment changes along with other attribute changes" do
old_photo = @thing.photo
@thing.name = 'new'
- @thing.photo = uploaded_file
+ @thing.photo = uploaded_file('test.jpg')
@thing.changes.should == {
'name' => ['old', 'new'],
'photo' => [old_photo, @thing.photo],
}
end
end
describe "when the record is saved and only attachments have been modified" do
before do
- @thing.photo = uploaded_file
+ @thing.photo = uploaded_file('test.jpg')
end
it "should not hit the database"
it "should still save the attachment files" do
@@ -632,11 +635,11 @@
end
describe "#save" do
before do
@thing.name = 'new'
- @thing.photo = uploaded_file
+ @thing.photo = uploaded_file('test.jpg')
end
it "should clear all changes" do
@thing.save
@thing.changes.should == {}
@@ -672,20 +675,20 @@
end
end
describe "when a file was assigned to the attachment" do
it "should update ATTACHMENT_updated_at" do
- @thing.photo = uploaded_file
+ @thing.photo = uploaded_file('test.jpg')
@thing.save.should be_true
@thing.photo_updated_at.should == Time.now
end
end
end
describe "when the record already exists" do
before do
- thing = Thing.create(:photo => uploaded_file('test.jpg', 'test.jpg'))
+ thing = Thing.create(:photo => uploaded_file('test.jpg'))
@thing = Thing.find(thing.id)
end
describe "when the attachment was not assigned to" do
it "should not update ATTACHMENT_updated_at" do
@@ -697,10 +700,10 @@
end
describe "when a new file was assigned to the attachment" do
it "should update ATTACHMENT_updated_at" do
warp_ahead 1.minute
- @thing.photo = uploaded_file('test.jpg', 'test.jpg')
+ @thing.photo = uploaded_file('test.jpg')
@thing.save.should be_true
@thing.photo_updated_at.should == Time.now
end
end
end