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.

Creating tickets<br />

Alternative validation syntax<br />

You can use a slightly differently named method call to accomplish the same thing<br />

here:<br />

validates_presence_of :title<br />

validates_presence_of :description<br />

Some people prefer this syntax because it’s been around for a couple of years; others<br />

prefer the newer style. It’s up to you which to choose. A number of other<br />

validates_* methods are available.<br />

And I press "Create Ticket"<br />

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

And I should see "Description is too short"<br />

The final line here is written this way because you do not know what the validation<br />

message is, but you’ll find out later. To implement this scenario, add another option<br />

to the end of the validation for the description in your Ticket model, as shown in<br />

the following listing.<br />

Listing 5.7 app/models/ticket.rb<br />

validates :description, :presence => true,<br />

:length => { :minimum => 10 }<br />

If you go into rails console and try to create a new Ticket object by using create!,<br />

you can get the full text for your error:<br />

irb(main):001:0> Ticket.create!<br />

ActiveRecord::RecordInvalid: ... Description is too short<br />

(minimum is 10 characters)<br />

That is the precise error message you are looking for in your feature. When you run<br />

bin/cucumber features/creating_tickets.feature again, you see that all three scenarios<br />

are now passing:<br />

3 scenarios (3 passed)<br />

25 steps (25 passed)<br />

You should ensure that the rest of the project still works. Because you have both features<br />

and specs, you should run the following command to check everything:<br />

rake cucumber:ok spec<br />

The summary for these two tasks together is 2<br />

9 scenarios (9 passed)<br />

62 steps (62 passed)<br />

# and<br />

5 examples, 0 failures, 4 pending<br />

2 The summary is altered to cut down on line noise with only the important parts shown.<br />

107

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

Saved successfully!

Ooh no, something went wrong!