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.

480 CHAPTER 17 Engines<br />

This is really helpful for Test::Unit, but you can also cannibalize it for your RSpec.<br />

Let’s create a spec directory and put in it a spec /spec_helper.rb, which contains similar<br />

content to the test/test_helper.rb file:<br />

# Configure Rails Environment<br />

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

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

require 'rspec/rails'<br />

__FILE__)<br />

Rails.backtrace_cleaner.remove_silencers! B Remove backtrace cleaner<br />

# Load support files<br />

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

It’s about identical, except you’ve replaced the require to rails/test_help with<br />

rspec/rails. This spec/spec_helper.rb file will be loaded by RSpec when you run<br />

your tests.<br />

One thing to note is the line Rails.backtrace_cleaner.remove_silencers! B.<br />

Rails has an automated backtrace cleaner, which it uses to shorten the resulting backtrace<br />

from errors so that it’s easier to track down. You don’t need this silencing going<br />

on in your tests, and so you can remove it using remove_silencers!.<br />

You need to add one more thing to the end of spec/spec_helper.rb, which will reset<br />

your database back to a clean slate once all of your tests have finished running. It will<br />

do this by running the test’s database queries inside a transaction, which is then rolled<br />

back at the end of the test. That would be this little configuration option:<br />

RSpec.configure do |config|<br />

config.use_transactional_fixtures = true<br />

end<br />

Without this, test data would accumulate in your database, leading to undesired<br />

results.<br />

Your next step in replacing Test::Unit with RSpec is to move the test/dummy directory<br />

to spec/dummy. This folder is the dummy application for your engine and contains<br />

the all-important config/environment.rb file, which is required in spec/<br />

spec_helper.rb.<br />

With all this code moved over, you can remove the test directory completely,<br />

because you don’t need it any more. The test/dummy directory itself is still referenced<br />

in the engine in a couple of places, and you’ll need to replace these references with<br />

the new spec/dummy location.<br />

The first of these locations is the Rakefile file at the root of the engine. This file is<br />

loaded when any rake task is executed and is responsible for loading those tasks. You<br />

need to replace this line in Rakefile<br />

APP_RAKEFILE = File.expand_path("../test/dummy/Rakefile", __FILE__)<br />

with this:<br />

APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)

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

Saved successfully!

Ooh no, something went wrong!