back

Testing picture / file uploads with webrat and cucumber

Webrat implements the step:

1
2
3
4
5

When /^I attach the "(.*)" file at "(.*)" to "(.*)"$/ do |type, path, field|
  attach_file(field, path, type)
end

So, for example:

1
2
3
4
5

Given X
When I attach the "image/jpg" file at "features/support/picture.jpg" to "avatar_image"
Then Y

March 25, 2009

  1. Ben Mabey says:

    Nice. You should submit this as a patch to Cucumber, for the default webrat step definitions.

    What would be nice though is if the attach_file command made the type optional and it inferred it by looking at the file...

  2. Mischa says:

    @ben, I was all set to do both of those, but it turns out that they are both already done. attach_file's content type already is optional, and there is the following step already in the cukes rails gen in head:

    When /^I attach the file at "([^\"]*)" to "([^\"]*)"$/ do |path, field|
    attach_file(field, path)
    end