Spec_server not reloading model classes


#1

Hi,

I’m using the latest rspec-rails 1.2.2 with rails 2.3.2, and I’m
trying to get it to work with spec_server. This is on Mac/Leopard,
with the stock ruby, all gems installed in /Library/Ruby/. I have
configured:

config/environments/test.rb

config.cache_classes = false

spec/spec.opts

–drb

The tests run nice and fast, so I know they’re running against the
spec_server, but when I make a change to my model which should toggle
the result of a test, it doesn’t change until I restart the
spec:server. I have tried running it with combinations of:

rake spec:server:start
script/spec_server

script/autospec
autospec

The specs run, but no reloading makes it useless. Any ideas?

Thanks,
Andrew V.


#2

Andrew V. wrote:

spec/spec.opts

–drb

Do you notice reloading occurring in dev mode (with cache-classes =
false) with the same class?

Are you explicitly using require anywhere?

Scott


#3

I just tried it with:

$ rake RAILS_ENV=development spec:server:start
$ RAILS_ENV=development script/autospec

And it looks the same. (If I change the model file, the tests re-run
but it obviously doesn’t get reloaded: even if I add a syntax error,
it still runs.)

Not sure where you mean about the explicit require. All my specs files
have require spec_helper at the top, which is how they come out of the
box…

Thanks for your help,
Andrew


#4

Andrew V. wrote:

I just tried it with:

$ rake RAILS_ENV=development spec:server:start
$ RAILS_ENV=development script/autospec

Actually I meant poking around your app with a browser in dev mode :wink:

I’m pretty sure that rails will still try to run the test environment,
even with RAILS_ENV set to a different value, anyway.

And it looks the same. (If I change the model file, the tests re-run
but it obviously doesn’t get reloaded: even if I add a syntax error,
it still runs.)

Not sure where you mean about the explicit require. All my specs files
have require spec_helper at the top, which is how they come out of the
box…

I was thinking of any explicit requires which may occur in app/ and
lib/.

AFAIK, this is more of a rails loading issue than an rspec one.

Scott

The tests run nice and fast, so I know they’re running against the
spec_server, but when I make a change to my model which should toggle
the result of a test, it doesn’t change until I restart the
spec:server. I have tried running it with combinations of:


#5

It appears this is a bug, with an apparently simple fix in
spec_server.rb (rails 2.3 compatibility):

https://rspec.lighthouseapp.com/projects/5645/tickets/759

However, now that it’s reloading my app, it’s not reloading the setup
from my fixture/factory gems and throwing up a bunch of errors on re-
run. (I’ve tried it with both factory_girl and machinist now.) These
are required in my spec_helper.rb:

config.gem ‘notahat-machinist’, :lib => ‘machinist’, :source =>
http://gems.github.com
require File.dirname(FILE) + ‘/blueprints’

or:

config.gem ‘thoughtbot-factory_girl’, :lib => ‘factory_girl’, :source
=> ‘http://gems.github.com

automatically loaded from spec/factories/*.rb

Not sure where to look for a solution.


#6

On Mar 29, 1:40 am, Scott T. removed_email_address@domain.invalid wrote:

Actually I meant poking around your app with a browser in dev mode :wink:

Ok, I get it, I wasn’t sure what you were driving at! :wink: Yes,
everything is reloading fine in the browser under development.

I’m pretty sure that rails will still try to run the test environment,
even with RAILS_ENV set to a different value, anyway.

The RAILS_ENV is being set conditionally in my script files, except
script/autospec where it isn’t defined.
ENV[“RAILS_ENV”] ||= ‘test’

I was thinking of any explicit requires which may occur in app/ and lib/.

Nothing out of the ordinary…

AFAIK, this is more of a rails loading issue than an rspec one.

I just tried this on a clean new rails app, and the reloading still
doesn’t work when running spec_server and autospec with --drb. It
works fine when I’m not using spec_server, but then it reloads all of
rails each time. Works fine, except when using spec_server.

The rspec site says:
[http://rspec.info/rails/runners.html]

Note that there are some classes and modules that, by default, won’t get reloaded
by Rails, which means they won’t be reloaded by spec_server. There are a number
of strategies available for coercing Rails to reload files every time in a given
environment. See the Rails documentation for more information.

Does that include my app/models? I wouldn’t have thought so… Another
post on this mailing list seemed to suggest those don’t need manual
reloading and “just work”.

$ gem list rails
rails (2.3.2)
$ gem list rspec
rspec (1.2.2)
rspec-rails (1.2.2)
$ rails test_spec_server && cd test_spec_server
$ script/plugin install git://github.com/dchelimsky/rspec-rails.git
$ script/generate rspec
$ script/generate rspec_model car name:string

edit config/environments/test.rb => config.cache_classes = false

edit spec/spec.opts => --drb

$ rake spec:server:start
$ script/autospec

1 example, 0 failures

edit app/models/car.rb => add invalid syntax!

1 example, 0 failures

Can anyone confirm/reproduce? (Or else tell me what I’m doing wrong?)

Thanks,
Andrew V.


#7

On Sun, Mar 29, 2009 at 6:14 PM, Andrew V. removed_email_address@domain.invalid wrote:

config.gem ‘notahat-machinist’, :lib => ‘machinist’, :source =>
http://gems.github.com
require File.dirname(FILE) + ‘/blueprints’

or:

config.gem ‘thoughtbot-factory_girl’, :lib => ‘factory_girl’, :source
=> ‘http://gems.github.com

automatically loaded from spec/factories/*.rb

In spec_helper?


#8

On May 1, 2009, at 7:04 AM, David C. wrote:

In spec_helper?
Hi David,

Sorry, that wasn’t clear the way I wrote it. The config.gem directive
was actually in the rails/environments/test.rb file. Then I required
the blueprints.rb file in spec_helper (or in factory_girl’s case it
automatically loads the factory files from spec/factories). These
steps showed that rails model reloading wasn’t working in 1.2.2 with
rails 2.3.2:

edit spec/spec.opts => --drb

$ rake spec:server:start
$ script/autospec

1 example, 0 failures

edit app/models/car.rb => add invalid syntax!

1 example, 0 failures

I had given up on spec_server for the time being. The ticket is
resolved now, and I just gave these steps another try with a new app
using updated versions. Reloading the app classes for each run works
fine out of the box now: I can get it to return a success or failure
when I change the model on the fly.

However, the problem comes when adding Factory Girl:

config/environments/test.rb

add:

config.gem “thoughtbot-factory_girl”, :lib => ‘factory_girl’

spec/factories/car.rb

Factory.define :car do |f|
f.name “Jetta”
end

spec/models/car_spec.rb

it “should create a new instance given valid attributes” do
car = Factory :car
car.name.should == ‘Jetta’
end

It runs fine the first time against drb, but on all subsequent runs I
get the following error until I restart spec_server. None of the lines
in the stacktrace say “rspec” but I was also getting the same error
with Machinist too, so it’s not unique to Factory Girl, and I’m
guessing something to do with the reloading.

TypeError in ‘Car should create a new instance given valid attributes’
can’t dup NilClass
/Library/Ruby/Gems/1.8/gems/activerecord-2.3.2/lib/active_record/
base.rb:2189:in dup' /Library/Ruby/Gems/1.8/gems/activerecord-2.3.2/lib/active_record/ base.rb:2189:inscoped_methods’
/Library/Ruby/Gems/1.8/gems/activerecord-2.3.2/lib/active_record/
base.rb:2193:in current_scoped_methods' /Library/Ruby/Gems/1.8/gems/activerecord-2.3.2/lib/active_record/ base.rb:2176:inscoped?’
/Library/Ruby/Gems/1.8/gems/activerecord-2.3.2/lib/active_record/
base.rb:2440:in send' /Library/Ruby/Gems/1.8/gems/activerecord-2.3.2/lib/active_record/ base.rb:2440:ininitialize’
/Library/Ruby/Gems/1.8/gems/thoughtbot-factory_girl-1.2.1/lib/
factory_girl/proxy/build.rb:5:in new' /Library/Ruby/Gems/1.8/gems/thoughtbot-factory_girl-1.2.1/lib/ factory_girl/proxy/build.rb:5:ininitialize’
/Library/Ruby/Gems/1.8/gems/thoughtbot-factory_girl-1.2.1/lib/
factory_girl/factory.rb:284:in new' /Library/Ruby/Gems/1.8/gems/thoughtbot-factory_girl-1.2.1/lib/ factory_girl/factory.rb:284:inrun’
/Library/Ruby/Gems/1.8/gems/thoughtbot-factory_girl-1.2.1/lib/
factory_girl/factory.rb:237:in create' /Library/Ruby/Gems/1.8/gems/thoughtbot-factory_girl-1.2.1/lib/ factory_girl/factory.rb:268:insend’
/Library/Ruby/Gems/1.8/gems/thoughtbot-factory_girl-1.2.1/lib/
factory_girl/factory.rb:268:in default_strategy' /Library/Ruby/Gems/1.8/gems/thoughtbot-factory_girl-1.2.1/lib/ factory_girl.rb:20:inFactory’
./spec/models/car_spec.rb:11:
/Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:in
gem_original_require' /Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:inrequire’
/Library/Ruby/Gems/1.8/gems/activesupport-2.3.2/lib/active_support/
dependencies.rb:156:in require' /Library/Ruby/Gems/1.8/gems/activesupport-2.3.2/lib/active_support/ dependencies.rb:521:innew_constants_in’
/Library/Ruby/Gems/1.8/gems/activesupport-2.3.2/lib/active_support/
dependencies.rb:156:in `require’

Andrew V.


#9

On Mar 30, 1:14 am, Andrew V. removed_email_address@domain.invalid wrote:

config.gem ‘notahat-machinist’, :lib => ‘machinist’, :source =>
http://gems.github.com
require File.dirname(FILE) + ‘/blueprints’

or:

config.gem ‘thoughtbot-factory_girl’, :lib => ‘factory_girl’, :source
=> ‘http://gems.github.com

automatically loaded from spec/factories/*.rb

Not sure where to look for a solution.

Same problem for me with factory_girl and rspec_rails 1.2.6, work fine
the first time but get a bunch of errors the second.

Any solution? Thanks


#10

Did anyone ever figure out the factory_girl / machinist issues? I am
having the same problems and can figure out how to fix it for the life
of me. The first run works fine, then afterwards I get a bunch of these
errors:

No blueprint for class Venue

Any ideas? Thanks!


#11

Ben J. wrote:

Did anyone ever figure out the factory_girl / machinist issues? I am
having the same problems and can figure out how to fix it for the life
of me. The first run works fine, then afterwards I get a bunch of these
errors:

No blueprint for class Venue

Any ideas? Thanks!

Actually, I decided to make a tiny release. See this commit:

http://github.com/smtlaissezfaire/fixturereplacement/commit/6aba70c51f0701820b46dc687e965b43e21eca41

Scott


#12

Scott T. wrote:

I don’t know about Machinist or Factory Girl. Basically, here’s the
deal:

If those libraries use require instead of load, you are screwed. Same
goes for autoload’ing.
Yep. You might want to check out Spork[1]. It is a drop-in replacement
for spec_server that uses Kernel.fork instead of reloading classes to
ensure a clean app state. My coworker, Tim H., wrote it and it has
been working great for us so far. (There is a little setup, so not
quite drop-in but it just works with the --drb flag for rspec.)

  1. http://github.com/timcharper/spork/tree/master

-Ben


#13

Ben J. wrote:

Did anyone ever figure out the factory_girl / machinist issues? I am
having the same problems and can figure out how to fix it for the life
of me. The first run works fine, then afterwards I get a bunch of these
errors:

No blueprint for class Venue

Any ideas? Thanks!

I don’t know about Machinist or Factory Girl. Basically, here’s the
deal:

If those libraries use require instead of load, you are screwed. Same
goes for autoload’ing.

If those libraries internally use load, or load_dependency, and you have
cache_classes = false in your dev env, you could try manually reloading
it.

Here’s FixtureReplacement in script/console (@ version 2.1) after
reload! is called:

http://gist.github.com/116422


#14

Scott T. wrote:

Any ideas? Thanks!
(There is a little setup, so not quite drop-in but it just works with
the --drb flag for rspec.)

  1. http://github.com/timcharper/spork/tree/master

-Ben

That’s absolutely awesome.
Have there been any attempts to generalize this, so that mongrel will
fork on each request (a la shotgun)? (I’m still stuck on rails 2.0.2,
so a non-rack based reloading scheme would be great).

Well, so Spork was really created with testing in mind. It is more
general purpose than spec_server though. You can use it with any Ruby
project, not just Rails. You can also potentially use it with any other
testing framework that adds support for it (I’ve been meaning to do this
for Cucumber). I know that Tim’s original work with Kernel.fork was
actually dealing with Mongrel but I’m not sure what the exact details of
it are. I’ll try to find out for you though.

-Ben


#15

Ben M. wrote:

rspec.)

  1. http://github.com/timcharper/spork/tree/master

-Ben

That’s absolutely awesome.

Have there been any attempts to generalize this, so that mongrel will
fork on each request (a la shotgun)? (I’m still stuck on rails 2.0.2,
so a non-rack based reloading scheme would be great).

Scott


#16

On Sun, May 24, 2009 at 10:17 PM, Ben M. removed_email_address@domain.invalid wrote:

Well, so Spork was really created with testing in mind. It is more general
purpose than spec_server though. You can use it with any Ruby project, not
just Rails. You can also potentially use it with any other testing
framework that adds support for it (I’ve been meaning to do this for
Cucumber). I know that Tim’s original work with Kernel.fork was actually
dealing with Mongrel but I’m not sure what the exact details of it are.
I’ll try to find out for you though.

Spork seems to have the same problem that I have with spec_server: it
doesn’t reload classes I change. So if I’m doing TDD between a model and
its
spec, it doesn’t help.

///ark


#17

Mark W. wrote:

On Sun, May 24, 2009 at 10:17 PM, Ben M. removed_email_address@domain.invalid wrote:

Spork seems to have the same problem that I have with spec_server: it
doesn’t reload classes I change. So if I’m doing TDD between a model and
its
spec, it doesn’t help.

///ark

I have the same problem. I like spork, but I had to restart the spork
server anytime I made a change to a model file. The only way I know how
to avoid this is to not proload the rails env, which kind of defeats the
purpose.


#18

On May 26, 2009, at 1:22 PM, Mark W. wrote:

Spork seems to have the same problem that I have with spec_server:
it doesn’t reload classes I change. So if I’m doing TDD between a
model and its spec, it doesn’t help.

I noticed the same thing. It must be a bug in spork, correct?

Scott


#19

On May 26, 2009, at 2:31 PM, Ben M. wrote:

with any Ruby project, not just Rails. You can also potentially

connection is ready.

Keep in mind that the code in Spork.prefork must not require any of
your models inadvertently. If your environment file does this or a
plugin does then your models will not be reloaded. Look over your
spec_helper file and try to see who is requiring your models
initially. We have really only tested this on is our apps where Tim
and I work… So, there could very well be bugs we need to iron out.

Sweet! Seems to be working great now.

Looks like the README has changed over the weekend - previously, I had
been starting the spec server with script/spec_server. Running spork
directly seems to work just fine.

Thanks a TON!

Scott


#20

Scott T. wrote:

use it with any other testing framework that adds support for it

I noticed the same thing. It must be a bug in spork, correct?

Scott

What does your spec_helper look like?

It should be something like:

require ‘rubygems’
require ‘spork’

Spork.prefork do
ENV[“RAILS_ENV”] = “test”
require File.expand_path(File.dirname(FILE) +
“/…/config/environment”)

Load other stuff that only needs to be setup once and never reloaded

(i.e. rspec and configuring it)
end

Spork.each_run do
ActiveRecord::Base.establish_connection # make sure that the db
connection is ready.
require File.expand_path(RAILS_ROOT + “/spec/fixjour_builders.rb”) #
You could use FixtureReplacement, FactorGirl, etc…

Spec::Runner.configure do |config|
config.include Fixjour
include SharedSpecHelpers
include DatabaseHelpers
include AuthenticatedTestHelper

end
end

Keep in mind that the code in Spork.prefork must not require any of your
models inadvertently. If your environment file does this or a plugin
does then your models will not be reloaded. Look over your spec_helper
file and try to see who is requiring your models initially. We have
really only tested this on is our apps where Tim and I work… So, there
could very well be bugs we need to iron out.

-Ben