且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何找到 Dragonfly 使用 Cucumber/Capybara 生成的图像?

更新时间:2022-10-21 20:49:45

Answering my own question, after talking to Dragonfly's author (he's working on making this easier):

#route
match '/media/:dragonfly/:file_name', :to => Dragonfly[:images]  

#model
class Store
  include MongoMapper::Document
  key :photo_uid, String
  key :photo_name, String
  image_accessor :photo
end

#view
<%= image_tag @store.photo.thumb('274x207#').url(:suffix => "/#{@store.photo_name}") if @store.photo %>

#cucumber
Then I should see the image "my_photo.png"

Then /^I should see the image "(.+)"$/ do |image|  
  page.should have_xpath("//img[contains(@src, "#{image}")]")
end

The key is adding an [attachment]_name field to the model, which Dragonfly populates automatically, and then passing it on as a suffix to url(). And the routes needs to allow for a :file_name param besides the generated dragonfly identifier.