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.

482 CHAPTER 17 Engines<br />

s.add_development_dependency "rspec-rails", "~> 2.5"<br />

s.add_development_dependency "capybara"<br />

You can run bundle install to install these two gems as dependencies because you’ve<br />

got the gemspec method call in Gemfile. Once that command is done, then you’ve got<br />

what you need in terms of gems.<br />

Your next move is to set up Capybara to be used with RSpec. In spec/spec_helper<br />

.rb there’s this line:<br />

Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }<br />

This line will require all the files with a .rb extension within the spec/support directory<br />

or its subdirectories. You can use this to load Capybara for your tests by creating the<br />

spec/support folder and putting a file called spec/support/capybara.rb inside of it with<br />

this content:<br />

require 'capybara/rails'<br />

require 'capybara/dsl'<br />

RSpec.configure do |c|<br />

c.include Capybara, :example_group => {<br />

:file_path => /\bspec\/integration\//<br />

}<br />

end<br />

The capybara/rails sets up Capybara to be used for your dummy application (which<br />

loads your engine), while the capybara/dsl gives you all the helpful Capybara methods<br />

that you’re going to need in your tests.<br />

The final chunk of code here includes the Capybara module within all tests that<br />

are within the spec/integration directory. This should provide a pretty big clue as to<br />

where your integration tests are going for your engine!<br />

That’s all that you’ve got to do for setting up Capybara and RSpec. Let’s make a<br />

commit for this:<br />

git add .<br />

git commit -m "Remove Test::Unit, replace with RSpec + Capybara"<br />

Now why don’t we get into some real coding?<br />

17.5 Writing your first engine feature<br />

When they first start writing a Rails application, many people will attempt to create a<br />

forum system. 17 This is a great place to start, as a lot of people have used forum systems<br />

18 and therefore they understand the basic concepts of how they work.<br />

Generally, on the home page there’s a list of forums that have topics inside of them<br />

and then posts inside of those topics. Each topic and post are created by a user, and<br />

there’s a wide gamut of conversation (and, on larger forums, a great deal of trolling)<br />

that goes on in them.<br />

17 One of the authors attempted such a project and it grew into rboard: http://github.com/radar/rboard.<br />

18 Such as PHPbb and VBulletin.

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

Saved successfully!

Ooh no, something went wrong!