Testing File Upload with Rails 3.2, RSpec, and Paperclip

37 days ago

I spent hours struggling to simulate a file upload (in my case, a photo) in my controller tests. In the model, it’s simple:

describe Photo do
  before(:each) do
    user = Factory(:user)
    attr = {
      :photo => File.new(File.join(Rails.root, 'spec', 'support', 'test.png')),
      :imageable_id => @user.id,
      :imageable_type => 'User'
    }
  end
...

However, attempting the same technique in the controller fails. Why? I still don’t know, but I suspect it has something to do with the lack of browser involvement in the process.

The Solution

describe PhotosController do
  before(:each) do
    @attr = {
      :photo => Rack::Test::UploadedFile.new(File.join(Rails.root, 'spec', 'support', 'test.png'), 'image/png')
    }
  end
...

It turns out to be that simple. After hours of messing around, debugging, and random experimentation, I finally just took a peek at the Diaspora project’s specs. Duh. Made me feel stupid, but glad to be moving on…

John (Jack) Brown

,

Comment?

---

Useful Web Development Resources

41 days ago

HAML

HAML Language Reference

Ruby

Time & Date

Options for strftime

John (Jack) Brown

,

Comment?

---

Another new site!

91 days ago

Yes, I’ve changed things up yet again. Alas alack. As interested as I am, I just can’t help coming to the conclusion that I’m just not a very good blogger. Sorry to those of you I’ve disappointed.

Not only have I changed up my blog, but it’s also moved homes, and should find itself in permanent residence here at brownjohnf.com.

Be welcome, and stay tuned for updates.

John (Jack) Brown

,

Comment?

---

A weekend of fun!

91 days ago

It’s now Wednesday, and I’m beginning to feel rested again after a long weekend of fun with Susan, Emily, and Suleyman. They all came up Friday night, got in rather late, and everyone was up early Saturday ready for a big breakfast. Emily and I went out to the market and got vats of vegetables, some of which were then processed into an enormous scrambled egg / vegetable extravaganza (delicious) accompanied by french toast, and excellent Caffe D’arte coffee I brought back with me.

We lazed around for a while during the late morning (we didn’t finish breakfast until close to noon), then went up to the Casino Supermarket for some ice cream and shopping. This was Suley’s first trip to Dakar, so the supermarket was also a bit of a sightseeing outing. Finally, after a quick pit stop back at the apartment, we all trooped off to a nearby beach with some bread and cheese. Not a bad day at all, and we topped the whole thing off with a creamy basil pesto dish, along with salad and wine.

Sunday was a quick trip downtown to N’Ice Cream, then out to another, much larger beach, where we met a bunch of other Volunteers and Senegalese friends for lunch. Grilled fish, and possibly the best I’ve had in-country. That night, we went for something a little simpler and had rotisserie chicken with salad and frites.

Susan and Suley left Monday morning, and Emily left yesterday morning, early.

John (Jack) Brown

,

---

Another attempt at communication...

91 days ago

For those of you who made some attempt to follow my blog, I’m sure you’re well aware that I wasn’t very good about updating it. Now, living in Dakar, I thought I’d take another stab at it. Maybe before you spread the word about this new site, give me a bit to see how this round goes!

John (Jack) Brown

,

---

« Older