27.02.2013 Views

Rails%203%20In%20Action

Rails%203%20In%20Action

Rails%203%20In%20Action

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

210 CHAPTER 8 More authorization<br />

The load method works in a similar fashion to require, loading and executing the<br />

code inside the file. One difference, however, is that load expects the given string (or<br />

Pathname) to be the full path, with the extension, to the file.<br />

First write a feature to ensure that when the seed data is loaded, you can sign in<br />

with the email admin@ticketee.com and the password password and you can get to the<br />

TicketeeBeta project. Put this feature in features/seed.feature, and write it as shown in<br />

the following listing.<br />

Listing 8.21 features/seed.feature<br />

Feature: Seed Data<br />

In order to fill the database with the basics<br />

As the system<br />

I want to run the seed task<br />

Scenario: The basics<br />

Given I have run the seed task<br />

And I am signed in as "admin@ticketee.com"<br />

Then I should see "Ticketee Beta"<br />

It’s a pretty basic feature, but your seed file will be equally basic. Before you create it,<br />

however, you should define the first step of this scenario. You can get the definition<br />

for this step by running bin/cucumber features/seed.feature. The step definition<br />

looks like this:<br />

Given /^I have run the seed task$/ do<br />

pending # express the regexp above with the code you wish you had<br />

end<br />

Put this step definition in a new file called features/step_definitions/<br />

application_steps.rb because it doesn’t really tie in with the other step files you’ve<br />

defined. If you have more steps like this, you can put them into this file later too. The<br />

code for this file is simple:<br />

Given /^I have run the seed task$/ do<br />

load Rails.root + "db/seeds.rb"<br />

end<br />

Now when you run your feature again, it can’t find the user your seed data is supposed<br />

to create:<br />

And I am signed in as "admin@ticketee.com"<br />

Couldn't find User with email = admin@ticketee.com<br />

➥(ActiveRecord::RecordNotFound)<br />

It can’t find this user because you haven’t yet created one for this scenario. This user<br />

should be created by the db/seeds.rbfile. Open this file now, and add a couple of lines<br />

to create this user, set the user up as an admin, and confirm the user, as shown in the<br />

following listing.

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

Saved successfully!

Ooh no, something went wrong!