Why is RSpec so slow under Rails?

Whenever I run rspec tests for my Rails application, it takes forever and a day of overhead before it starts running tests. Why is rspec so slow? Is there a way to speed up the initial loading of Rails or highlight the part of my Rails application that I need (for example, only ActiveRecord) so that it does not download absolutely everything in order to run several tests?

+43
ruby ruby-on-rails rspec
Sep 15 '08 at 20:46
source share
11 answers

You have the opportunity to speed up your script/spec calls by running script/spec_server in a separate terminal window, and then adding the -X option to your specifications.

+22
Sep 15 '08 at 22:38
source share

I definitely suggest checking out spork.

http://spork.rubyforge.org/

This is specifically addressed in the rail tutorial and provides a workaround that makes it easy to deal with spork in rails 3.0 (at the moment, spork is not 3 rails out of the box). Of course, if you are not on rails 3.0, then you should be good to go.

Part of a tutorial showing how to run spork in rails 3.0

http://railstutorial.org/chapters/static-pages#sec:spork

Checking when spork rails 3.0 is ready

http://www.railsplugins.org/plugins/440-spork

+32
Oct 27 '10 at 20:59
source share

Why is rspec so slow? because he downloads all the environement, loads the fixtures and all this jazz.

Is there a way to speed up the initial loading of Rails , you can try using mocks instead of relying on a database, this is really correct for unit testing and will certainly speed up your unit tests. An add-on using a dedicated server, as @ Scott Matthewman points out, can help, just like the zentest autotest mentioned by @ Marc-Andre Lafortune

Is there a way to highlight the part of my Rails application that I need (for example, only ActiveRecord stuff), so it doesnโ€™t download absolutely everything to run a few tests? What about this p>

 rake test:recent 

I'm not sure how the rspec task integrates with this, but you can certainly use a test: a recent task as a template for executing rspec tags if.

 rake test:rspec:recent 

doesn't exist yet

+4
Sep 16 '08 at 16:33
source share

because it downloads all the environement, downloads fixtures and all this jazz.

The real culprit is that you run it with rake spec , it runs the db:test:prepare task .

This task leaves the entire test database and recreates it from scratch. It seems funny to me, but what it does (the same thing happens when rake:test:units , etc.) starts.

You can easily get around this using the spec application that rspec installs as part of the rspec gem.

Like this:

 cd railsapp spec spec # run all specs without rebuilding the whole damn database spec spec/models # run model specs only cd spec spec controllers/user* # run specs for controllers that start with user 
+4
Sep 17 '08 at 1:14
source share

I think the zen experience you're looking for is to run spec_server and autospec in the background, with the result being almost instantaneous when the file is saved.

However, I am having problems so that these two programs can communicate.

I found an explanation here :

I noticed that autotest does not send commands to spec_server. Instead, it reloads the entire Rails environment and the plugin application every time it runs. This will cause the autotest to run much slower than the script server, because when you run the script / spec, the specification is sent to spec_server, which already has a Rails environment running and ready to go. if you happen to install a new plugin or something like that, then you need to restart spec_server.

But how do we fix this problem? I assume that this will be due to downloading ZenTest and changing the code for the autotest program, but you do not have time to try it right now.

+4
Nov 12 '08 at 0:16
source share

Do you use this over Rails? If so, this is not RSpec initialization, which slows down, then Rails. Rails must initialize the entire codebase and yours before running the specs. Well, this is not necessary, but it is. RSpec works pretty fast for me under my small railless projects.

+3
Sep 15 '08 at 20:47
source share

Running tests can be very slow, because the whole rail environment is loading (try script / console), and only then all tests will be run. You should use autotest , which supports booting the environment and checks which files you are editing. When you edit and save a file, only those tests that depend on them will be executed automatically and quickly.

+3
Sep 15 '08 at 22:58
source share

If you are using a Mac, I recommend using Rspactor over autotest, as it uses much less resources to poll modified files than autotest. There is a full version of Cocoa

RSpactor.app

or the gem version that I support on Github

 sudo gem install pelle-rspactor 

As long as they do not speed up individual rspec tests, they feel much faster because they automatically run the affected spec for a second when you click save.

+2
Sep 16 '08 at 1:27
source share

As with rspec-rails-1.2.7, spec_server is deprecated in favor of the spork gem.

+2
Feb 17 '10 at 21:54
source share

The main reason is that, for some reason, a hang on windows is required.

Acceleration Tips: It seems that spork now works with windows.

You can try "quick_require" which caches locations:

http://github.com/rdp/faster_require

GL -rp

+2
Jun 03 '10 at 17:11
source share

If you're in a Windows environment, then there is probably little to be done, as Rails seems to run slowly under Windows. I had the same experience on Windows, and I had to move my installation to the Linux virtual machine to make it really invisible (I also used autotest).

+1
Sep 16 '08 at 9:05
source share



All Articles