How to assert that image has been uploaded with Capybara?

我们两清 提交于 2020-01-07 06:36:23

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/44304883/how-to-assert-that-image-has-been-uploaded-with-capybara

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!