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.

478 CHAPTER 17 Engines<br />

2. Goes through middleware<br />

stack until...<br />

It’s the same cycle, except this time the routing code has determined this request<br />

should be handled by the engine, rather than the application.<br />

We’ve talked long enough about the theory of an engine, and you’ve learned some<br />

key concepts. An engine is a miniature application that provides some functionality,<br />

and it’s routed like a normal application would be, providing you’ve mounted it in<br />

your application’s config/routes.rb file.<br />

It’s time to put all this theory into practice.<br />

17.4 Setting up a testing environment<br />

Before you can get down to writing any tests, you’re going to need to set up the environment<br />

to do that. For a change, you’re not going to be writing tests for your engine<br />

using Cucumber. Instead, you’re going to be using RSpec and Capybara. It’s always<br />

good to get a different perspective on testing, as not everyone agrees on the One True<br />

Way TM of doing things. The syntax for this RSpec and Capybara is still easy going, however,<br />

as shown by this example:<br />

require 'spec_helper'<br />

1. Request comes in for<br />

GET /forem/forums<br />

Middleware #1<br />

Middleware #n<br />

3. Routes receive request<br />

Browser<br />

Server<br />

... Application Engine<br />

Routes<br />

4. What route is it?<br />

GET /forem/forums<br />

7. Response goes out<br />

describe "topics" do<br />

it "creating a new one" do<br />

visit topics_path<br />

click_link "New Topic"<br />

fill_in "Subject", :with => "First topic!"<br />

6. Engine responds<br />

5. That route goes to<br />

the engine!<br />

Figure 17.4<br />

Routing cycle of an engine

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

Saved successfully!

Ooh no, something went wrong!