On 2010-04-25 3:43 AM, Patrick J. Collins wrote:
Hi everyone,

I have a test for importing vcards, and I was finding that each time it was
being run, it was generating new photos and since it was using the test
database, it was actually overwriting real photos that were being stored in the
file system (they are named based on the record's id).  So it was apparent to
me that I need to use mocking to intercept Photo.create so that it doesn't
really happen...


...snip...
Any ideas?

Thanks..

Patrick J. Collins
http://collinatorstudios.com

Hi Patrick,

See http://gist.github.com/378489 for how I ended up specing a file upload. This isn't the exact same situation, but maybe you will get some ideas that will help. And there may be a better way, but this seems to work and is fairly easy for me understand.

Here's a little info on the app to shed some light on what I'm doing in the spec. I am working on an app that has a "media library" that users can upload into. There are four types of media: audio, video, images, and documents. During the upload process, I try to determine the mime type automatically, and from that, the type of media, but I do allow those things to be set manually. I have a run-time constant (APP_CONFIG[:media_root_path]) that defines where the files should be on the file system, and I have to change that during the test to make sure development files don't get overwritten. That's accomplished with the call to set_app_config, which is really nothing more than replacing a constant. Its definition is

    def set_app_config(new_app_config)
        saved_app_config = APP_CONFIG.dup
Kernel::silence_warnings { Object.const_set(:APP_CONFIG, new_app_config ) }
        return saved_app_config
    end

Let me know if you have any questions.

Peace,
Phillip

_______________________________________________
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users

Reply via email to