27.02.2013 Views

Rails%203%20In%20Action

Rails%203%20In%20Action

Rails%203%20In%20Action

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Attaching many files<br />

asset objects while creating a new Ticket model. The best part is that the code to do all<br />

of this remains the same in the controller.<br />

You need to modify the scenario for creating a ticket with an attachment in your<br />

Creating Tickets feature so it uploads two additional files.<br />

9.2.1 Two more files<br />

Let’s take the scenario for creating a ticket with an attachment from features/<br />

creating_tickets.feature and add these two additional file upload fields so the entire<br />

scenario looks like the following listing.<br />

Listing 9.6 File attachment scenario, features/creating_tickets.feature<br />

Scenario: Creating a ticket with an attachment<br />

When I fill in "Title" with "Add documentation for blink tag"<br />

And I fill in "Description" with "The blink tag has an undocumented<br />

➥speed attribute"<br />

And I attach the file "spec/fixtures/speed.txt" to "File #1"<br />

And I attach the file "spec/fixtures/spin.txt" to "File #2"<br />

And I attach the file "spec/fixtures/gradient.txt" to "File #3"<br />

And I press "Create Ticket"<br />

Then I should see "Ticket has been created."<br />

And I should see "speed.txt" within "#ticket .assets"<br />

And I should see "spin.txt" within "#ticket .assets"<br />

And I should see "gradient.txt" within "#ticket .assets"<br />

In this scenario, you attach three files to your ticket and assert that you see them<br />

within the assets element, which was previously called #ticket .asset but now has<br />

the pluralized name of #ticket .assets.<br />

You download only the one file here and check for the content. It’s not at all likely<br />

that the other two files wouldn’t work in the same way given that they’re interpreted<br />

and rendered identically.<br />

Now run this single scenario using bin/cucumber features/creating_tickets<br />

.feature:36. It should fail on the first Attach the File step, because you renamed the<br />

label of this field:<br />

And I attach the file "spec/fixtures/speed.txt" to "File #1"<br />

cannot attach file, no file field with id, name,<br />

or label 'File #1' found (Capybara::ElementNotFound)<br />

To get this step to pass, you can change the label on the field in app/views/tickets/<br />

_form.html.erb to "File #1":<br />

<br />

<br />

<br />

<br />

While you’re changing things, you may as well change app/views/tickets/<br />

show.html.erb to reflect these latest developments. First, change the if around the<br />

221

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!