Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

carrierwave_direct in model specs #163

Closed
sebastianzillessen opened this issue Jan 22, 2015 · 2 comments
Closed

carrierwave_direct in model specs #163

sebastianzillessen opened this issue Jan 22, 2015 · 2 comments

Comments

@sebastianzillessen
Copy link

Hi there,

I try to get my specs working. I have a model Attachment which has a file attribute attachment. Now I want to provide a mocked attachment through my factory (I use FactoryGirl). I use Ruby (2.1.3) on Rails (4.0.2). I Upload the files to amazon s3.

What I tried so far is:

# config/initializers/carrierwave.rb
if (Rails.env.test?)
  Fog.mock!
end
CarrierWave.configure do |config|
  config.fog_credentials = {
      provider: "AWS",
      aws_access_key_id: s3['access_key_id'],
      aws_secret_access_key: s3['secret_access_key'],
      region: s3['region']
  }
  config.fog_directory = s3['bucket']
  config.max_file_size = 500.megabytes
end


# factories/models/attachments.rb
include CarrierWaveDirect::Test::Helpers
FactoryGirl.define do
  factory :attachment do
    ...
    attachment { sample_key(AttachmentUploader.new, filename: "#{SecureRandom.hex}file.pdf") }
  end
end

This seems to work somehow, at least I get a valid attachment entry with some properties set:

 #<AttachmentUploader:0x007faaa595fae0 @model=#<Attachment id: 141, ... , >, @mounted_as=:attachment, @key="uploads/f97a1258-40b3-4850-8841-0bd17c1621b2/${filename}", @storage=#<CarrierWave::Storage::Fog:0x007faaa58fb6a8 @uploader=#<AttachmentUploader:0x007faaa595fae0 ...>>> 

But when I try to check if the attachment is present or not every location is nil:

a.attachment.path
 => nil 
a.attachment.file
 => nil 

Any ideas how I can fix this and how it is done correctly?

Thanks a lot!

@vemv
Copy link

vemv commented Jun 12, 2016

Well, you are mocking Fog so it's reasonable that path and file are nil?

Personally I use real S3 in tests (with Capybara), using a dedicated bucket.

@p8 p8 closed this as completed Jul 2, 2018
@p8
Copy link
Collaborator

p8 commented Jul 2, 2018

Possibly related to #111

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants