LoadError: "Expected admin/known_ip.rb to define Admin::KnownIp", ignores set_fixture_class #2572

Closed
alexeymuranov opened this Issue Aug 17, 2011 · 10 comments

Comments

Projects
None yet
5 participants
Contributor

alexeymuranov commented Aug 17, 2011

I am getting a

LoadError: Expected .../app/models/admin/known_ip.rb to define Admin::KnownIp

when running a Unit Test, because i actually define Admin::KnownIP in models/admin/known_ip.rb.
I specified the class with set_fixture_class, but it didn't help despite that it helps in a situation when KnownIP is not namespaced.

Steps to reproduce

Create a rails 3.1 application with

app/models/admin/known_ip.rb:

class Admin::KnownIP < ActiveRecord::Base
  set_table_name :admin_known_ips
end

test/unit/admin/known_ip_test.rb:

require 'test_helper'

class Admin::KnownIPTest < ActiveSupport::TestCase
  test "the truth" do
    assert true
  end
end

In test/test_helper.rb:

fixtures :all
set_fixture_class 'admin/known_ips' => Admin::KnownIP  

Bug

Then

$ ruby -Itest test/unit/admin/known_ip_test.rb

gives:

Admin::KnownIPTest:
    ERROR the truth (0.02s) 
          LoadError: Expected .../dep_test2/app/models/admin/known_ip.rb to define Admin::KnownIp
          .../activesupport-3.1.0.rc6/lib/active_support/dependencies.rb:486:in `load_missing_constant'

Inconsistency

However(!) this error does not show up if KnownIP is not put inside Admin module: adding set_fixture_class in that case suffices.

The error also does not show up if fixtures :all is commented out.

Another way to make the error go away is to rename the model file known_ip.rb to foo.rb.
Then there is no error regardless whether i use set_fixture_class or not.
I can tell that the Admin::KnownIP class is still loaded from models/admin/foo.rb because if i use set_fixture_class with a wrong class name, it raises an error.

This is with rails 3.1.

Contributor

alexeymuranov commented Aug 19, 2011

I want to explain that there are actually 2 issues i am pointing out in this report:

  1. set_fixture_class seems to not work the same with a namespaced class as it does otherwise.
  2. LoadError seems inconsistent: why admin/known_ip.rb is expected to define Admin::KnownIp, but admin/foo.rb is not expected to define Admin::Foo in the described above situation?
    Why to expect a class to be denied somewhere if this class is not mentioned anywhere and does not exist?

pdjota commented Dec 1, 2011

Hi, did you run your fix with all the activerecord tests? I got 46 failures.

Contributor

alexeymuranov commented Dec 1, 2011

No, i also do not have PostgreSQL ready for tests. I will try to test with sqlite. Thank you for trying this.

brodock commented Jan 21, 2012

Can you, please, tell if this fix will be only on Rails 3.1 or will be on 3.0 too?

Contributor

alexeymuranov commented Jan 21, 2012

I can rebase it to make a fix for 3.0 too, no problem, but so far my PR #4482 has not been merged.

Contributor

alexeymuranov commented Jan 26, 2012

@tenderlove: can it be fixed in 3.2 please?

@alexeymuranov was this fix put into 3.2? I'm experiencing the LoadError issue.

Contributor

alexeymuranov commented Jul 17, 2012

@vquigley: no, i think @tenderlove is busy :).

@alexeymuranov and such is the way of open source & free software ;) As a workaround I will just require the files.

Contributor

alexeymuranov commented Jul 18, 2012

amen!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment