x-yuri
x-yuri

Reputation: 18863

How to assert that image has been uploaded with Capybara?

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?

Upvotes: 0

Views: 273

Answers (1)

Tom Lord
Tom Lord

Reputation: 28305

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.

Upvotes: 1

Related Questions