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.

def edible?<br />

!expired<br />

end<br />

Behavior-driven development<br />

When you run rspec spec again, both your specs will pass:<br />

..<br />

2 examples, 0 failures<br />

Let’s go back in to lib/bacon.rb and remove the self. from the expired! method:<br />

def expired!<br />

expired = true<br />

end<br />

If you run rspec spec again, you’ll see your second spec is now broken:<br />

Failure/Error: Bacon expired!<br />

expected edible? to return false, got true<br />

Tests save you from making mistakes such as this. If you write the test first and then<br />

write the code to make the test pass, you have a solid base and can refactor the code to<br />

be clearer or smaller and finally ensure that it’s still working with the test you wrote in<br />

the first place. If the test still passes, then you’re probably doing it right.<br />

If you change this method back now<br />

def expired!<br />

self.expired = true<br />

end<br />

and then run your specs using rspec spec, you’ll see that they once again pass:<br />

..<br />

2 examples, 0 failures<br />

Everything’s normal and working once again, which is great!<br />

That ends our little foray into RSpec for now. You’ll use it again later when you<br />

develop your application. If you’d like to know more about RSpec, The RSpec Book:<br />

Behavior-Driven Development with RSpec, Cucumber, and Friends (David Chelimsky et al.,<br />

Pragmatic Bookshelf, 2010) is recommended reading.<br />

2.3.2 Cucumber<br />

Listing 2.18 bacon/lib/bacon.rb<br />

For this section, we retire the Bacon example and go for something more formal with<br />

Cucumber.<br />

NOTE This section assumes you have RSpec installed. If you don’t, use this<br />

command to install it: gem install rspec.<br />

Whereas RSpec and Test::Unit are great for unit testing (testing a single part), Cucumber<br />

is mostly used for testing the entire integration stack.<br />

35

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

Saved successfully!

Ooh no, something went wrong!