LoginSignup
22
19

More than 5 years have passed since last update.

RSpec で capybara を使用してファイルアップロードをする場合

Last updated at Posted at 2013-11-14

使用した各バージョン
* ruby 2.0.0p247
* Rails 4.0.0
* rspec-rails 2.14.0
* capybara 2.1.0

RSpec で rails のテストを書く場合、form にテキストを擬似的に入力するときに capybara を使うと思います。
テキストフィールドの場合、fill_in を使うことは知ってたんですが、

html.erb
<%= form_for(@content) do |f| %>
  <div class="field">
    <%= f.label :upload_file %><br>
    <%= f.file_field :upload_file %>
  </div>
  <div class="actions">
    <%= f.submit "Upload" %>
  </div>
<% end %>

この html のようにファイルアップロードの場合どうやってやるのか分からなかったので調べてみました。

結論は、ファイルアップロードの場合は、attach_file を使用する。

rspec
attach_file "Upload file", "/tmp/test.txt"

ここら辺の使い方は、capybara のドキュメントに書いてあります。
http://rubydoc.info/github/jnicklas/capybara/master/Capybara/Node/Actions

チェックボックスとかラジオボタンの操作など色々用意されてるんですね。
capybara 便利!!!!!

22
19
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
22
19