[rspec-users] spec_server not reloading model classes

79 views
Skip to first unread message

Andrew Vit

unread,
Mar 29, 2009, 2:51:33 AM3/29/09
to rspec...@rubyforge.org
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 Vit
_______________________________________________
rspec-users mailing list
rspec...@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users

Scott Taylor

unread,
Mar 29, 2009, 3:12:46 AM3/29/09
to rspec-users
Andrew Vit wrote:
> 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
>
>
Do you notice reloading occurring in dev mode (with cache-classes =
false) with the same class?

Are you explicitly using require anywhere?

Scott

Andrew Vit

unread,
Mar 29, 2009, 4:05:42 AM3/29/09
to rspec...@rubyforge.org
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

> > rspec-us...@rubyforge.org


> >http://rubyforge.org/mailman/listinfo/rspec-users
>
> _______________________________________________
> rspec-users mailing list

> rspec-us...@rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users

Scott Taylor

unread,
Mar 29, 2009, 4:40:49 AM3/29/09
to rspec-users
Andrew Vit 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 ;)

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

Andrew Vit

unread,
Mar 29, 2009, 6:15:48 AM3/29/09
to rspec...@rubyforge.org

On Mar 29, 1:40 am, Scott Taylor <sc...@railsnewbie.com> wrote:
> Actually I meant poking around your app with a browser in dev mode ;)

Ok, I get it, I wasn't sure what you were driving at! ;-) 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 Vit
_______________________________________________
rspec-users mailing list

rspec...@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users

Andrew Vit

unread,
Mar 29, 2009, 7:14:13 PM3/29/09
to rspec...@rubyforge.org
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.

> rspec-us...@rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users

Thibaud Guillaume-Gentil

unread,
May 1, 2009, 4:14:52 AM5/1/09
to rspec...@rubyforge.org
On Mar 30, 1:14 am, Andrew Vit <and...@avit.ca> wrote:
> 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 bothfactory_girland 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.

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

David Chelimsky

unread,
May 1, 2009, 10:04:15 AM5/1/09
to rspec-users
On Sun, Mar 29, 2009 at 6:14 PM, Andrew Vit <and...@avit.ca> wrote:
> 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

In spec_helper?

Andrew Vit

unread,
May 1, 2009, 1:16:19 PM5/1/09
to rspec-users
On May 1, 2009, at 7:04 AM, David Chelimsky wrote:

> On Sun, Mar 29, 2009 at 6:14 PM, Andrew Vit <and...@avit.ca> wrote:
>> 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
>
> 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:

>>> $ 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


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:in `scoped_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:in `scoped?'
/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:in `initialize'
/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:in `initialize'
/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:in `run'
/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:in `send'
/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:in `Factory'
./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:in `require'
/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:in `new_constants_in'
/Library/Ruby/Gems/1.8/gems/activesupport-2.3.2/lib/active_support/
dependencies.rb:156:in `require'

Andrew Vit

_______________________________________________
rspec-users mailing list
rspec...@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users

Ben Johnson

unread,
May 22, 2009, 6:55:07 PM5/22/09
to rspec...@rubyforge.org
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!
--
Posted via http://www.ruby-forum.com/.

Scott Taylor

unread,
May 22, 2009, 7:53:39 PM5/22/09
to rspec...@rubyforge.org
Ben Johnson 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

Scott Taylor

unread,
May 22, 2009, 7:41:43 PM5/22/09
to rspec...@rubyforge.org
Ben Johnson 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

Ben Mabey

unread,
May 24, 2009, 1:16:41 AM5/24/09
to rspec-users
Scott Taylor wrote:
> Ben Johnson 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.
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 Harper, 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

Scott Taylor

unread,
May 24, 2009, 5:30:54 AM5/24/09
to rspec-users
Ben Mabey wrote:
> Scott Taylor wrote:
>> Ben Johnson 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.
> 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 Harper, 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

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

Ben Mabey

unread,
May 25, 2009, 1:17:07 AM5/25/09
to rspec-users

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

Mark Wilden

unread,
May 26, 2009, 1:22:30 PM5/26/09
to rspec-users
On Sun, May 24, 2009 at 10:17 PM, Ben Mabey <b...@benmabey.com> 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

Ben Johnson

unread,
May 26, 2009, 1:32:44 PM5/26/09
to rspec...@rubyforge.org

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.

Scott Taylor

unread,
May 26, 2009, 1:33:44 PM5/26/09
to rspec-users
I noticed the same thing.  It must be a bug in spork, correct?

Scott

Ben Mabey

unread,
May 26, 2009, 2:31:44 PM5/26/09
to rspec-users

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.

Scott Taylor

unread,
May 26, 2009, 2:46:49 PM5/26/09
to rspec-users

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

Ben Mabey

unread,
May 26, 2009, 3:06:40 PM5/26/09
to rspec-users
>> "/../config/environment")


Ok cool. Seems like the bug may of been poor documentation... please
send a pull request if you think it can be improved. :)

-Ben

Scott Taylor

unread,
May 26, 2009, 3:10:18 PM5/26/09
to rspec-users

Tim Harper

unread,
Jun 2, 2009, 1:58:50 AM6/2/09
to rspec...@rubyforge.org
Ben J.,
Have you given up on this?  You're right - that defeats the purpose of Spork. You need to make sure that any code that loads code in your project does not make it in to your Spork.prefork block.  If you are specifically loading the models in your environment, that's your problem.  
 
Here's some possible options to resolve what you're experiencing:
 
1) Add spork blocks to your environment.rb to put portions that are loading code you develop on inside of a Spork.each_run block, and the rest of it in a Spork.prefork block.
 
2) modify your environment.rb file to only load libraries like gems and plug-ins, and set up autoload to load the rest of your classes as you need them. Alternatively, is this is a matter of pre-caching, you could choose not to load those classes preemptively in the test environment.
 

Zach Powell

unread,
Jun 10, 2009, 10:50:09 PM6/10/09
to rspec...@rubyforge.org
Ben Johnson 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 found that using

load File.expand_path(File.dirname(__FILE__) + "/blueprints.rb")

in the spec_helper.rb

did the trick.

Lenart Rudel

unread,
Oct 28, 2009, 5:54:09 PM10/28/09
to rspec...@rubyforge.org
Some plugins (like Thinking Sphinx) preload all the files in the app/
folder. You can see if this is the case by logging spork's output to a
file and see what requested the app/* files to load.

spork -d > spork.log

More details can be found here:
http://upstream-berlin.com/2009/07/27/kickstart-rspec-with-spork

Hope it helps someone! It took me an hour to solve this :)

Reply all
Reply to author
Forward
0 new messages