I’ve been doing quite a lot of Behaviour Driven Development using RSpec recently. However, I hit a problem topday, I was trying to test file uploads to Rick Olson’s attachment_fu plugin. First I thought all I had to do was to pass it a file handle or contents of a file, not so. The tests failed moaning of missing methods. I had a rummage around in the tests for the plugin itself.
A upload_file function is called in the attachment tests and defined in test/test_helper.rb. This in turn calls the Rails fixture_file_upload function. This creates a fake file upload which can then be used to create a new attachment.
The only thing is that all the files are uploaded to the public directory which leave it rather messy. In the code I’m using below told both the path_prefix for my attachment to be the tmp/test_images directory and deleted that after each test incarnation. The only problem with this is that it takes ages for the tests to run. :-(
TEST_DIR = "tmp/test_images"
before(:each) do
Image.attachment_options[:path_prefix] =
"#{TEST_DIR}/images"
@image = Image.create :uploaded_data =>
fixture_file_upload("test_image.jpg", "image/jpg")
end
after(:each) do
@image.destroy
FileUtils.rm_rf File.join(RAILS_ROOT, TEST_DIR)
end
November 1st, 2007 at 03:46 PM
Hi ant,
Thanks for the article. I’ve got a problem running your code, perhaps you could shed lights here.
My autotest returned the following error when it got to the line Image.create
Mock ‘Image_1012’ received unexpected message :save with (no args)
You have any ideas why?
Regards,
Sonny
January 14th, 2008 at 04:19 PM
Oh I didn’t know about the fixture_file_upload method, thanks!
Btw you can mock the actual save of the file and that will solve both of the problems you have:
January 15th, 2008 at 03:44 AM
You could also change the before and after to before(:all) and after(:all). It would reduce the overhead.