ruby-on-rails – 如何在Capybara / Rspec中存根回形针文件路径
发布时间:2020-12-17 02:06:36  所属栏目:百科  来源:网络整理 
            导读:对于某些应用程序,我使用Paperclip进行文件上传(实际上是dm-paperclip风格),以及Factory Girl,Rspec,Capybara进行测试. 我有一个非常简单的工厂用于“图片”模型,我按照 this post中的建议存储我的文件属性: FactoryGirl.define do factory :picture do tit
                
                
                
            | 
 对于某些应用程序,我使用Paperclip进行文件上传(实际上是dm-paperclip风格),以及Factory Girl,Rspec,Capybara进行测试. 
  我有一个非常简单的工厂用于“图片”模型,我按照 this post中的建议存储我的文件属性: FactoryGirl.define do
  factory :picture do
    title "My Picasso"
    description "It's like looking in a mirror."
    picture_file_file_name { 'spec/resources/img_1.jpg' }
    picture_file_content_type { 'image/jpg' }
    picture_file_file_size { 1024 }
  end
end在使用Capybara进行的各种功能测试中,我访问了模板具有Picture实例缩略图的页面: feature "List of Pictures",:js => true  do
  scenario "displays appropriately the index page of the pictures with pagination" do
    FactoryGirl.create_list(:picture,21)
    visit '/pictures'
    # And more testing...
  end
end其中一个模板中使用的部分示例: =  content_tag_for(:li,picture,:class => 'listed_picture') do
  = link_to picture_path(picture) do
    - if picture.picture_file?
      = image_tag picture.picture_file.url(:thumb)我现在遇到的问题是,每当我运行规范时,测试都会失败,因为缩略图网址没有匹配的路由: No route matches [GET] "/system/picture_files/1/thumb/img_1.jpg" 是否有任何方法可以使用Paperclip的辅助方法来使测试通过? 在此先感谢您的帮助! 解决方法
 我刚刚完成了这个过程.这就是我解决这个问题的方法. 
  
  首先,我在对象上创建了一个引用图像URL的方法,既遵守了Demeter的规律又使得更容易测试.对你来说,这看起来像是: #picture.rb
class Picture
...
  def picture_file_url(size = nil)
    picture_file.url(size)
  end
...
end现在我们准备在规范中存根Paperclip附件网址: describe "List of Pictures",:js => true  do
  it "displays appropriately the index page of the pictures with pagination" do
    let(:picture) { create(:picture) }
    allow(Picture).to receive(:picture_file_url) { "url" }
    visit '/pictures'
    # And more testing...
  end
end希望这可以帮助你或某人. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! | 
