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.

end<br />

Setting up a testing environment<br />

fill_in "Text", :with => "First post!"<br />

click_button "Create Topic"<br />

within "#flash_notice" do<br />

page.should have_content("Topic has been created!")<br />

end<br />

within ".forem_topic #posts .forem_post" do<br />

page.should have_content("First post!")<br />

end<br />

end<br />

An obvious downside for writing this is that it’s not as human-friendly as a Cucumber<br />

test is, but it’s close enough. Anybody with an extremely basic understanding of Ruby<br />

should be able to understand everything here apart from the CSS selectors, which are<br />

another class of their own. 15<br />

There are a couple of benefits to using Capybara directly over using it in conjunction<br />

Cucumber. First, it’s going to be faster because there’s no parsing of the steps as<br />

there is in Cucumber—it’s straight method calls to the very same methods that Cucumber<br />

uses. Second (and related), all the code for the feature is kept within one file.<br />

You’re going to need to make some changes to your engine to install RSpec and<br />

Capybara first.<br />

17.4.1 Removing Test::Unit<br />

At the moment in your application, you’ve got a test directory that uses Test::Unit for<br />

testing. We’ve avoided this throughout the book, and this chapter’s not going to be<br />

any exception to that rule. 16 You’re going to switch this over to using RSpec.<br />

Inside this test directory, there is the test/test_helper.rb file that contains this<br />

content:<br />

# Configure Rails Environment<br />

ENV["RAILS_ENV"] = "test"<br />

require File.expand_path("../dummy/config/environment.rb", __FILE__)<br />

require "rails/test_help"<br />

Rails.backtrace_cleaner.remove_silencers!<br />

# Load support files<br />

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

15 You could create a helper method here to help clarify what these mean, such as this assert_seen method<br />

from the real forem engine:<br />

https://github.com/radar/forem/blob/87092925e8f7092723e07e0adbae44ad96a45d01/spec<br />

/integration/posts_spec.rb#L28<br />

16 For two reasons. First, the authors prefer RSpec. Second, RSpec is preferred by the majority of people in the<br />

community. There are still pockets of resistance though.<br />

479

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

Saved successfully!

Ooh no, something went wrong!