Options for a continuous integration environment with RoR?

Hi all,

I'm in the process of setting up cruisecontrolrb with RCov as our
continuous integration server. We use rSpec for our unit tests, and
AFAIK all the integration tests and system tests are done offline
(i.e. scripted but not automated).

I've heard that many people use Cerberus or "straight" CruiseControl
with Ruby. Is there a consensus over which one is better, if any?
And how do people automate their system and integration tests in
Rails? Is there a standard way to do this?

Will.

Hi Will,

I can't speak to server-oriented tools like CC, but you might want to
check out ZenTest [1] for continuous testing on your own development
machine. It has a tool called "autotest" that will run in the
background and re-run your tests every time you save a file.

I believe all you have to do is a: `sudo gem install ZenTest` and then
cd to the root directory of your rails project and type "autotest".

ted

[1] http://www.zenspider.com/ZSS/Products/ZenTest/

I recommend using cruisecontrol. It should be fairly easy to set up and will run all of your tests every time you commit a file. It has the added option (a plugin, perhaps?) of sending a message through jabber to somebody, or a list of somebodies, or even an email.

Autotest is good for your own box, but when cruisecontrol sends out insults humiliating a team member because they committed a broken build, it gives them the incentive not to do it again!

I recommend using cruisecontrol. It should be fairly easy to set up
and will run all of your tests every time you commit a file. It has
the added option (a plugin, perhaps?) of sending a message through
jabber to somebody, or a list of somebodies, or even an email.

Autotest is good for your own box, but when cruisecontrol sends out
insults humiliating a team member because they committed a broken
build, it gives them the incentive not to do it again!

We've been using cruisecontrol.rb for a little while now and it's
great. We've added a little tweak so that it fires on a subversion
post-commit hook rather than polling and everytime a build is broken
it emails us.

Fred

I'm using CruiseControl.rb as well for all my Rails apps and also for
pure Ruby apps (including a datawarehouse project), and I'm also using
Rspec like you. I've had small issues setting up growl notifications,
but that's all !

cheers

Thibaut

Excellent, thanks!

Will.

I don't suppose there's any way to decouple unit tests from the
database in rspec for those models that extend ActiveRecord? I looked
at unit_record, but that seems to be for Test::Unit only.

If there isn't, I'm just going to label anything that extends
ActiveRecord::Base as an integration test.

Will.

Ah yes, I’m after something for this too. The problem is that I have 600 tests and they take 300 seconds to run. I also stumbled across unit_record. I’m sure there’s something rspec’y out there.

Looks like the best option is here:

http://muness.blogspot.com/2006/12/unit-testing-rails-activerecord-classes.html

As far as I can tell, the big problem with isolation is that
ActiveRecord depends on the database to tell it what fields it has.
I'm not sure that a database is much better than using a config file
for schema information in this case...

Will.

D'oh. Haven't been reading my own posts: schema.rb IS the config file
containing schema information in this case. So unitrecord is the way
to go.

Will.

And how do we implement this into Rspec?

Move your rspec directories to follow the "jfields" method:

spec/
  unit/
     models/
     controllers/
  functional/
     models/
     controllers/

Edit spec_helper.rb to be the following:

ENV["RAILS_ENV"] = "test"
require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
require 'spec'
require 'spec/rails'

Create a unit/unit_spec_helper.rb with the following:

require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
require 'unit_record'

# Disconnect ActiveRecord from the database
# http://blog.jayfields.com/2007/09/rails-how-we-test.html
# http://www.dcmanges.com/blog/rails-unit-record-test-without-the-database
ActiveRecord::Base.disconnect!

And then change the model specs and controller specs to use
'unit_spec_helper.rb' instead of spec_helper.rb.

There is one outstanding bug that I haven't resolved: request,
response and controller have gone away, and you are now required to
declare @request, @response and @controller in the before(:each)
method.

Other than that, it seems to work fine. :slight_smile:

Will.

There is one outstanding bug that I haven't resolved: request,
response and controller have gone away, and you are now required to
declare @request, @response and @controller in the before(:each)
method.

Okay, it looks like moving the controller specs out of the normal
directory tree means there's no autorecognition of what's a
controller, model, etc. So if you're using this scheme, you have to
define the spec type by hand:

describe FooController, "Foo Controller", :type => :controller do

end

describe FooModel, "Foo Model", :type => :model do

end

And so on.

Will.

I use mock objects and other test doubles with RSpec. A unit test
should test a unit, not the full pathway from ActiveRecord through the
RAID array.

///ark

Can you explain how you mock out the ActiveRecord::Base stuff?

Will.

Mocking is definitely more useful at the functional level, where you
can simply set up stubs and expectations for the models, then make
sure the controller calls them correctly, meanwhile having its own
behavior tested.

At the unit test level, I use mocks for complicated objects that the
object under test has to deal with. As for the object under test
itself, it's just as easy to mock any ActiveRecord::Base methods that
you don't want to go through the database. You might even mock the
connection.

///ark