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.

2.3.1 RSpec<br />

Bacon.edible?.should be_true<br />

end<br />

end<br />

Behavior-driven development<br />

The benefits of writing tests like this are that clients can understand precisely what<br />

the test is testing and then use these steps in acceptance testing; 5 a developer can<br />

read what the feature should do and then implement it; and finally, the test can be<br />

run as an automated test. With tests written in DSL, you have the three important elements<br />

of your business (the clients, the developers, and the code) all operating in the<br />

same language.<br />

RSpec is an extension of the methods already provided by Test::Unit. You can even<br />

use Test::Unit methods inside of RSpec tests if you wish. But we’re going to use the<br />

simpler, easier-to-understand syntax that RSpec provides.<br />

Cucumber tests are written in a language called Gherkin, which goes like this:<br />

Given I am reading a book<br />

When I read this example<br />

Then I should learn something<br />

Each line indicates a step. The benefit of writing tests in the Gherkin language is that<br />

it’s closer to English than RSpec is, making it even easier for clients and developers to<br />

read.<br />

RSpec is a BDD tool written by Steven R. Baker and now maintained by David Chelimsky<br />

as a cleaner alternative to Test::Unit, with RSpec being built as an extension to<br />

Test::Unit. With RSpec, you write code known as specs that contain examples, which are<br />

synonymous to the tests you know from Test::Unit. In this example, you’re going to<br />

define the Bacon constant and then define the edible? method on it.<br />

Let’s jump right in and install the rspec gem by running gem install rspec. You<br />

should see the following output:<br />

Successfully installed rspec-core-2.6.4<br />

Successfully installed rspec-expectations-2.6.4<br />

Successfully installed rspec-mocks-2.6.4<br />

Successfully installed rspec-2.6.4<br />

You can see that the final line says the rspec gem is installed, with the version number<br />

specified after the name. Above this line, you also see a thank-you message and,<br />

underneath, the other gems that were installed. These gems are dependencies of the<br />

rspec gem, and as such, the rspec gem won’t work without them.<br />

When the gem is installed, create a new directory for your tests called bacon anywhere<br />

you like, and inside that, create another directory called spec. If you’re running<br />

a UNIX-based operating system such as Linux or Mac OS X, you can run the<br />

mkdir -p bacon/spec command to create these two directories. This command will<br />

5 A process whereby people follow a set of instructions to ensure a feature is performing as intended.<br />

29

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

Saved successfully!

Ooh no, something went wrong!