如何断言该图片已通过Capybara上传? [英] How to assert that image has been uploaded with Capybara?

查看:77
本文介绍了如何断言该图片已通过Capybara上传?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个表单,用户可以在其中创建带有图像的帖子。我想确保所附的图像显示在下一页:

 访问网址
fill_in the_form
click_on'创建'
assert_selector'.post'
post = Post.first
img = page.find'.post .image'
assert_equal post.file.thumb.url ,URI(img [:src])。path

但是我被告知要声明数据库对象应避免进行系统测试。我该怎么办?

解决方案

只要后端没有复杂文件重命名,您就已经知道填充表单时上传的文件名:

  fill_in the_form 

因此,您可以断言该页面包含具有该名称的图像(也许使用 xpath )。



如果重命名文件(例如用连字符替换空格),则可以(理想情况下)选择一个不变的文件名,或在测试中重命名。 / p>

Let's say I have a form where user can create a post with an image attached to it. I want to make sure the image attached is displayed on the next page:

visit url
fill_in the_form
click_on 'Create'
assert_selector '.post'
post = Post.first
img = page.find '.post .image'
assert_equal post.file.thumb.url, URI(img[:src]).path

But I'm told asserting against database objects in system tests is to be avoided. What do I do then?

解决方案

So long as there's no "complex" file renaming happening on the backend, you already know the uploaded filename when populating the form:

fill_in the_form

Therefore, you could assert that the page contains an image with this name (perhaps using an xpath).

If there is trivial file renaming (e.g. replacing spaces with hyphens), then you could either (ideally) just choose a filename that does not change, or reproduce the renaming in your test.

这篇关于如何断言该图片已通过Capybara上传?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆