Rack::Test resulting in ActiveRecord::AssociationTypeMismatch
Asked Answered
P

2

10

I have a problem when running all of my specs.

    ActiveRecord::AssociationTypeMismatch:
       Affiliate(#2154746360) expected, got Affiliate(#2199508660)

It would appear that my models are being loaded twice.

I have isolated the problem to be introduced with Rack::Test's requirement to define an "app" method.

require 'rack/test'
include Rack::Test::Methods

# app method is needed for rack-test
def app
  Rails.application
end

If I comment out Rails.application my rack specs do not work, but all of my other specs work fine. The use of Rails.application in the "app" method introduces the error above.

If I run my specs individually, everything works. I am preloading my environment with Spork and I think that the models are loaded first by Spork and then they are redefined when Rails.application is called in my "app" method.

Any ideas on how I can resolve this problem? I am not sure if there is another way to set my Rails app in the "app" method.

Pappas answered 1/4, 2011 at 22:13 Comment(4)
We've been running into precisely the same issue, and have been quite unsuccessful in sussing it out. Have you had any luck?Coparcenary
Seems like there's some action on the factory_girl Google Group on this issue: groups.google.com/group/factory_girl/browse_thread/thread/…Coparcenary
Michael, I have not had any luck with it so far. I have just avoided running the offending specs with the rest of my specs. It is a pain.Pappas
I did end up solving this, but I don't know exactly what change in my refactoring did the trick. I think it's possible the problem was that my Rspec config block was outsite of Spork's prefork, but that doesn't really sit right with me. If it's helpful, I can go through my changes and try to isolate what actually fixed it.Coparcenary
P
0

I am no longer having this problem anymore. I updated my gems. Rails was updated from 3.0.5 to 3.0.7 and I would guess that may have been the gem update that fixed my problem. Either way with newer versions of gems, my problem is fixed.

Pappas answered 6/5, 2011 at 0:42 Comment(0)
N
1

From source code for Rails.application:

# File railties/lib/rails.rb, line 34
def application
  @@application ||= nil
end

This means that Rails.application returns the same object every time. Maybe this is the problem - running multiple tests on the same Rails app clashes with objects.

Some tutorials set tests up like this:

def app
  Rails::Application
end

Whereas others do it like this:

def app
  ActionController::Dispatcher.new
end

Both of which create new object for each call to app.

EDIT: Just noticed from logs that ActionController::Dispatcher.new is marked as deprecated.

Newhouse answered 4/5, 2011 at 7:56 Comment(1)
Thanks, but after updating my gems things seem to have sorted their way out.Pappas
P
0

I am no longer having this problem anymore. I updated my gems. Rails was updated from 3.0.5 to 3.0.7 and I would guess that may have been the gem update that fixed my problem. Either way with newer versions of gems, my problem is fixed.

Pappas answered 6/5, 2011 at 0:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.