Fast Rails Tests Corey Haines

Confreaks · 14K views · 2012 · 32:54

Open on YouTube

Testing, debugging & developer toolsWeb developmentSoftware architecture & designtalkdated

Why Stack Overflow recommends it

Stack Overflow users link it when asking or answering how to speed up slow Rails and RSpec suites, especially by isolating tests from the framework instead of relying on tools like Spork.

Recommended by 3 people across 3 different months, 2012 to 2014.

What it is

A conference talk by Corey Haines at a Ruby conference, recorded by Confreaks, on keeping Rails unit test suites fast. He shows techniques such as testing plain Ruby objects outside the Rails environment and the design improvements that follow.

What people said

Recommends Christopher Manning · 2012-02 · Stack Overflow · on “Strategy for combining rails and non-rails testing?”

Corey Haines [LINK] gives talks on fast rails tests [this video] which could be helpful for you. Here is an example app that demonstrates how you could layout tests that don't require rails [LINK] (be sure to switch the branch to no_rails to see the final layout). i.e. create extensions in lib , test them from spec_no_rails , call those extensions from your model, and run tests with time rspec -fd -I lib/your_extension/ spec_no_rails/

Recommends Jason Swett · 2013-04 · Stack Overflow · on “Test a base (abstract) controller”

I wonder if you could break your controller's functions out into a PORO and test that functionality the same way Corey Haines does in his "fast test" video: [this video]

Recommends Brad Werth · 2014-10 · Stack Overflow · on “Reducing n+1 queries using the Bullet and RSpec gems”

The most effective way is not at all. There may be some legitimate advantages to reducing n+1 queries in tests, most obviously to speed up overall execution time. It is quite possible, however, that you are either testing too much [LINK] or making less significant gains than what is possible [this video] . I also usually find it unappealing to write additional code to help prop up tests, rather than to contribute to the overall value of the application. Please allow me to propose an alternate use of your time. Only unit test to the absolute minimum of your comfort level. I personally like to focus on complex methods involving validation related issues and money or other math, you may have differe

Cites bkeepers · 2012-06 · Stack Overflow · on “Testing: how to focus on behavior instead of implementation without losing speed?”

I think all three opinions are still completely valid. Ryan and I were struggling with the maintainability of mocking, while David felt the maintenance tradeoff was worth it for the increase in speed. But these tradeoffs are symptoms of a deeper problem, which David alluded to in 2007: ActiveRecord. The design of ActiveRecord encourages you to create god objects that do too much, know too much about the rest of the system, and have too much surface area. This leads to tests that have too much to test, know too much about the rest of the system, and are either too slow or brittle. So what's the solution? Separate as much of your application from the framework as possible. Write lots of small

Cites Jonah · 2012-06 · Stack Overflow · on “Rspec Still Slow On Windows Even With Spork”

I have followed this tutorial on speeding up rspec with spork [LINK] , and I am on a win7 x64 box with ruby 1.9.2 and rails 3.2.5. Everything is working, but test still execute slowly. Does spork simply not do much on windows because the OS doesn't support forking? Is there anything else I can do to speed things up? I also found this similar SO question [LINK] , and watched the video by Corey Haines on fast testing [this video] . I enjoyed the video, but I can't help feeling that something is off when the state of our tools (slow tests due to rails startup time, in this case) dictates how we structure our code. If that slow startup time didn't exist, would there be any need for his methods? On th

Cites Ruy Diaz · 2013-03 · Stack Overflow · on “Isolated testing of a presenter nested under a model namespace”

I have the following object structure in a Rails app: # app/models/model_a/model_b/model_c.rb class ModelA < ActiveRecord::Base class ModelB < ActiveRecord::Base class ModelC < ActiveRecord::Base end end end # app/presenters/model_a/model_b/presenter_for_model_c.rb class ModelA::ModelB::PresenterForModelC end Now, when I want to test PresenterForModelC in isolation with RSpec, I don't want to have to require 'spec_helper' in order to have a fast running test (à la Corey Haines [this video] or Gary Bernhardt [LINK] ), so instead I'm trying to load my presenter directly: # spec/presenters/model_a/model_b/presenter_for_model_c_spec.rb require_relative '../../../app/presenters/model_a/model_b/present