I am uploading an image to my Imagecollection model in using CarrierWave, and would like to test that when I upload the image, it is actually available online. And that when I delete the image, it is actually removed.
I’m using an S3 backend, so i’d like to test this in the model itself, without having to have any controller dependencies, or run integration tests. So I would need to construct the url, Issue an HTTP reequest, and tests its return code. This code doesn’t work, but is there a way to do something similar to the following:
describe "once uploaded" do
subject {Factory :company_with_images}
it "should be accessible from a URL" do
image_url = subject.images.first.image.url
get image_url # Doesn't work
response.should be_success # Doesn't work
end
end
EDIT:
I ended up adding this to my Gemfile
gem rest-client
And using the :fog backend for my tests. Ideally, I could change the backend during the test with something like
before do
CarrierWave.configure do |config|
config.storage = :fog
end
end
describe tests
end
after do
CarrierWave.configure do |config|
config.storage = :end
end
end
But that doesn’t seem to actually do anything.
describe "once uploaded" do
describe "using the :fog backend" do
subject {Factory :company_with_images}
# This test only passes beecause the S3 host is specified in the url.
# When using CarrierWave :file storage, the host isn't specified and it
# fails
it "should be accessible from a URL" do
image_url = subject.images.first.image.url
response = RestClient.get image_url
response.code.should eq(200)
end
end
describe "using the :file backend" do
subject {Factory :company_with_images}
# This test fails because the host isn't specified in the url
it "should be accessible from a URL" do
image_url = subject.images.first.image.url
response = RestClient.get image_url
response.code.should eq(200)
end
end
end
I ended up redefining the spec as follows
with this helper
and using the restclient gem