ruby-on-rails – 如何在CapybaraRspec中存根回形针文件路径

ruby-on-rails – 如何在CapybaraRspec中存根回形针文件路径,第1张

概述对于某些应用程序,我使用Paperclip进行文件上传(实际上是dm-paperclip风格),以及Factory Girl,Rspec,Capybara进行测试. 我有一个非常简单的工厂用于“图片”模型,我按照 this post中的建议存储我的文件属性: FactoryGirl.define do factory :picture do title "My Picasso" 对于某些应用程序,我使用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 }  endend

在使用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...  endend

其中一个模板中使用的部分示例:

=  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.rbclass 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...  endend

希望这可以帮助你或某人.

总结

以上是内存溢出为你收集整理的ruby-on-rails – 如何在Capybara / Rspec中存根回形针文件路径全部内容,希望文章能够帮你解决ruby-on-rails – 如何在Capybara / Rspec中存根回形针文件路径所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1269846.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-08
下一篇 2022-06-08

发表评论

登录后才能评论

评论列表(0条)

保存