Forum: RSpec spec_server not reloading model classes

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
F0987c97234fa9c6b26f796bdbdab037?d=identicon&s=25 Andrew Vit (Guest)
on 2009-03-29 09:01
(Received via mailing list)
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
369b9fd2a0d9e7cdbc60907a2a056ad9?d=identicon&s=25 Scott Taylor (Guest)
on 2009-03-29 09:31
(Received via mailing list)
Andrew Vit 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
F0987c97234fa9c6b26f796bdbdab037?d=identicon&s=25 Andrew Vit (Guest)
on 2009-03-29 10:10
(Received via mailing list)
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
369b9fd2a0d9e7cdbc60907a2a056ad9?d=identicon&s=25 Scott Taylor (Guest)
on 2009-03-29 10:53
(Received via mailing list)
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


>>>
>>>
>>
>>
>>> 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:
>>>
F0987c97234fa9c6b26f796bdbdab037?d=identicon&s=25 Andrew Vit (Guest)
on 2009-03-29 12:34
(Received via mailing list)
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
F0987c97234fa9c6b26f796bdbdab037?d=identicon&s=25 Andrew Vit (Guest)
on 2009-03-30 01:26
(Received via mailing list)
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.
Bfec3c8c618f8fa4732cbfeafda2f654?d=identicon&s=25 Thibaud Guillaume-Gentil (Guest)
on 2009-05-01 16:01
(Received via mailing list)
On Mar 30, 1:14 am, Andrew Vit <and...@avit.ca> 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
5d38ab152e1e3e219512a9859fcd93af?d=identicon&s=25 David Chelimsky (Guest)
on 2009-05-01 16:07
(Received via mailing list)
On Sun, Mar 29, 2009 at 6:14 PM, Andrew Vit <andrew@avit.ca> 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?
F0987c97234fa9c6b26f796bdbdab037?d=identicon&s=25 Andrew Vit (Guest)
on 2009-05-01 20:02
(Received via mailing list)
On May 1, 2009, at 7:04 AM, David Chelimsky 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: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
328f0bb678423fcea01ebe3b0edc74e6?d=identicon&s=25 Ben Johnson (benjohnson)
on 2009-05-23 00:55
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!
369b9fd2a0d9e7cdbc60907a2a056ad9?d=identicon&s=25 Scott Taylor (Guest)
on 2009-05-23 01:58
(Received via mailing list)
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/fixturereplaceme...

Scott
369b9fd2a0d9e7cdbc60907a2a056ad9?d=identicon&s=25 Scott Taylor (Guest)
on 2009-05-23 02:07
(Received via mailing list)
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
C694a032be7518a0d704318895f8fe1d?d=identicon&s=25 Ben Mabey (mabes)
on 2009-05-24 07:31
(Received via mailing list)
Scott Taylor 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 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
369b9fd2a0d9e7cdbc60907a2a056ad9?d=identicon&s=25 Scott Taylor (Guest)
on 2009-05-24 11:34
(Received via mailing list)
Ben Mabey 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
C694a032be7518a0d704318895f8fe1d?d=identicon&s=25 Ben Mabey (mabes)
on 2009-05-25 07:24
(Received via mailing list)
Scott Taylor 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
48641c4be1fbe167929fb16c9fd94990?d=identicon&s=25 Mark Wilden (Guest)
on 2009-05-26 19:27
(Received via mailing list)
On Sun, May 24, 2009 at 10:17 PM, Ben Mabey <ben@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
328f0bb678423fcea01ebe3b0edc74e6?d=identicon&s=25 Ben Johnson (benjohnson)
on 2009-05-26 19:32
Mark Wilden wrote:
> On Sun, May 24, 2009 at 10:17 PM, Ben Mabey <ben@benmabey.com> 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.
369b9fd2a0d9e7cdbc60907a2a056ad9?d=identicon&s=25 Scott Taylor (Guest)
on 2009-05-26 20:01
(Received via mailing list)
On May 26, 2009, at 1:22 PM, Mark Wilden 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
C694a032be7518a0d704318895f8fe1d?d=identicon&s=25 Ben Mabey (mabes)
on 2009-05-26 20:34
(Received via mailing list)
Scott Taylor 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
369b9fd2a0d9e7cdbc60907a2a056ad9?d=identicon&s=25 Scott Taylor (Guest)
on 2009-05-26 20:49
(Received via mailing list)
On May 26, 2009, at 2:31 PM, Ben Mabey 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
C694a032be7518a0d704318895f8fe1d?d=identicon&s=25 Ben Mabey (mabes)
on 2009-05-26 21:12
(Received via mailing list)
Scott Taylor wrote:
>>>>
>>>> Spork seems to have the same problem that I have with spec_server:
>>
>>
>> end
>
> 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
>


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

-Ben
369b9fd2a0d9e7cdbc60907a2a056ad9?d=identicon&s=25 Scott Taylor (Guest)
on 2009-05-26 21:30
(Received via mailing list)
On May 26, 2009, at 3:06 PM, Ben Mabey wrote:

>>>>>
>>>>>
>>> It should be something like:
>>> end
>>>  include AuthenticatedTestHelper
>>> Tim and I work... So, there could very well be bugs we need to
>> Scott
>>
>
>
> Ok cool.  Seems like the bug may of been poor documentation...
> please send a pull request if you think it can be improved.  :)

BTW:

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

Scott
63f259ca39670e260cd50dd71013663c?d=identicon&s=25 Tim Harper (timcharper)
on 2009-06-02 08:03
(Received via mailing list)
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.
7a8b4121ef187572e1d21d508d17f918?d=identicon&s=25 Zach Powell (babelian)
on 2009-06-11 04:50
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.
974b0625894a3b9fcd3e300ddb3f41bf?d=identicon&s=25 Lenart Rudel (lenart)
on 2009-10-28 22:54
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-rs...

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

Zach Powell 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 found that using
>
> load File.expand_path(File.dirname(__FILE__) + "/blueprints.rb")
>
> in the spec_helper.rb
>
> did the trick.
This topic is locked and can not be replied to.