# Testing fails with fixtures not when invoked directly

**URL:** <https://www.ruby-forum.com/t/testing-fails-with-fixtures-not-when-invoked-directly/64916>\
**Category:** Rails\
**Created:** [August 15, 2006, 2:20pm UTC](https://www.ruby-forum.com/t/testing-fails-with-fixtures-not-when-invoked-directly/64916 "2006-08-15T14:20:19Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![3nd3r](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/3/c4cdca/32.png) [@3nd3r](https://www.ruby-forum.com/u/3nd3r)\
**Post date:** [August 15, 2006, 2:20pm UTC](https://www.ruby-forum.com/t/testing-fails-with-fixtures-not-when-invoked-directly/64916/1 "2006-08-15T14:20:19Z")

</div>

I have three files organization.rb, company.rb, and department.rb. I  
want  
to ensure the department always has a company

require File.dirname( **FILE** ) + ‘/…/test\_helper’

class OrganizationTest \< Test::Unit::TestCase  
fixtures :organizations

def setup  
@smo = organizations(:smo)  
end

def test\_department\_company\_defaults\_to\_parent  
org = Department.new(:name =\> ‘company’,:cost\_code =\> ‘123’,  
:parent\_id  
=\> organizations(:root\_dept))  
assert org.valid?  
assert org.company\_id = organizations(:root\_dept).company\_id  
end

end

This fails when loading the fixtures file from rake but works if I do  
the  
test by itself  
ruby test/unit/test\_organization.rb =\> this works

rake =\> fails

D:\workspace\directory\>rake  
(in D:/workspace/directory)  
c:/ruby/bin/ruby -Ilib;test “c:/ruby/lib/ruby/gems/1.8/gems/rake-0.7.1  
/lib/rake/rake\_test\_load  
er.rb” “test/unit/organization\_test.rb” “test/unit/phone\_test.rb”  
Loaded suite c:/ruby/lib/ruby/gems/1.8/gems/rake-0.7.1  
/lib/rake/rake\_test\_loader  
Started  
EEE  
Finished in 0.14 seconds.

1. Error:  
test\_department\_company\_defaults\_to\_parent(OrganizationTest):  
NoMethodError: You have a nil object when you didn’t expect it!  
You might have expected an instance of Array.  
The error occurred while evaluating nil.[]

D:/workspace/directory/config/…/vendor/rails/activerecord/lib/active\_record/fixtures.rb:4  
81:in `organizations' ./test/unit/organization_test.rb:65:in`test\_department\_company\_defaults\_to\_parent’

1. Error:  
test\_department\_company\_defaults\_to\_parent(OrganizationTest):  
NoMethodError: You have a nil object when you didn’t expect it!  
You might have expected an instance of Array.  
The error occurred while evaluating nil.-

D:/workspace/directory/config/…/vendor/rails/activerecord/lib/active\_record/transactions.  
rb:107:in `decrement\_open\_transactions’

D:/workspace/directory/config/…/vendor/rails/activerecord/lib/active\_record/fixtures.rb:5  
45:in `teardown’

\*\*\*\*\*\*\*\*\*\* Files are as below \*\*\*\*\*\*\*\*\*\*\*

\*\*\*\*\*\* organizations.yml \*\*

smo:  
id: 1  
name: SLC Systems Management Office  
cost\_code: 8211  
routing:  
type: Department  
company\_id: 4

faa:  
id: 4  
name: Federal Aviation Administration  
type: Company

root\_dept:  
id: 5  
name: root  
type: Department  
company\_id: 4

child1:  
id: 6  
name: child1  
type: Department  
company\_id: 4  
parent\_id: 5

child2:  
id: 7  
name: child2  
type: Department  
company\_id: 4  
parent\_id: 5

\*\* organization.rb

# == Schema Information

# Schema version: 1

# 

# Table name: organizations

# 

# id :integer(11) not null, primary key

# name :string(255) default(), not null

# routing :string(10)

# cost\_code :string(10)

# url :string(255)

# phone :string(14)

# fax :string(14)

# parent\_id :string(255)

# type :string(255)

# company\_id :string(255)

# 

class Organization \< ActiveRecord::Base

validates\_presence\_of :name

end

\*\* company.rb \*\*

class Company \< Organization

has\_many :departments

end

\*\* department.rb \*\*

class Department \< Organization

acts\_as\_tree

# acts\_as\_list :scope =\> :parent\_id

validates\_presence\_of :company\_id

belongs\_to :company

before\_validation :set\_default\_company\_from\_parent

protected

def set\_default\_company\_from\_parent  
self.company\_id ||= self.parent.company\_id if self.parent  
end

end

---

<div class="post-metadata">

**Author:** ![3nd3r](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/3/c4cdca/32.png) [@3nd3r](https://www.ruby-forum.com/u/3nd3r)\
**Post date:** [August 15, 2006, 2:20pm UTC](https://www.ruby-forum.com/t/testing-fails-with-fixtures-not-when-invoked-directly/64916/2 "2006-08-15T14:20:51Z")

</div>

We’re having the same problem. Actually it’s more accurate to say we’re  
NOW  
having the same problem, after months of development.

Do you our anyone have any thoughts on this occurrence?

---

<div class="post-metadata">

**Author:** ![3nd3r](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/3/c4cdca/32.png) [@3nd3r](https://www.ruby-forum.com/u/3nd3r)\
**Post date:** [August 17, 2006, 11:48pm UTC](https://www.ruby-forum.com/t/testing-fails-with-fixtures-not-when-invoked-directly/64916/3 "2006-08-17T23:48:31Z")

</div>

I am using MySql on a windows development box.

---

<div class="post-metadata">

**Author:** ![3nd3r](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/3/c4cdca/32.png) [@3nd3r](https://www.ruby-forum.com/u/3nd3r)\
**Post date:** [August 17, 2006, 7:35pm UTC](https://www.ruby-forum.com/t/testing-fails-with-fixtures-not-when-invoked-directly/64916/4 "2006-08-17T19:35:55Z")

</div>

I still have this problem, any ideas?

Thanks in advance

Dominic

---------- Forwarded message ----------  
From: Brittain [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Date: Aug 14, 2006 9:56 PM  
Subject: [Rails] Re: Testing fails with fixtures not when invoked  
directly  
To: removed\_email\_address@domain.invalid

We’re having the same problem. Actually it’s more accurate to say we’re  
NOW  
having the same problem, after months of development.

Do you our anyone have any thoughts on this occurrence?

---

<div class="post-metadata">

**Author:** ![3nd3r](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/3/c4cdca/32.png) [@3nd3r](https://www.ruby-forum.com/u/3nd3r)\
**Post date:** [August 18, 2006, 3:45am UTC](https://www.ruby-forum.com/t/testing-fails-with-fixtures-not-when-invoked-directly/64916/5 "2006-08-18T03:45:44Z")

</div>

Dominic, sorry to say, nothing has changed. We’re also using MySQL on a  
windows box, rails 1.1.6.

The errors seem to indicate the fixture isn’t loading (complains of  
records  
with ID=xyz missing, even though they’re in the fixtures), but  
otherwise, no  
new info.

---

<div class="post-metadata">

**Author:** ![3nd3r](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/3/c4cdca/32.png) [@3nd3r](https://www.ruby-forum.com/u/3nd3r)\
**Post date:** [August 17, 2006, 9:30pm UTC](https://www.ruby-forum.com/t/testing-fails-with-fixtures-not-when-invoked-directly/64916/6 "2006-08-17T21:30:12Z")

</div>

On 8/17/06, Dominic S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I still have this problem, any ideas?
> 
> Thanks in advance
> 
> Dominic

Do you by any chance use SQL Server 2000?

Raf

---

<div class="post-metadata">

**Author:** ![3nd3r](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/3/c4cdca/32.png) [@3nd3r](https://www.ruby-forum.com/u/3nd3r)\
**Post date:** [August 18, 2006, 8:55am UTC](https://www.ruby-forum.com/t/testing-fails-with-fixtures-not-when-invoked-directly/64916/7 "2006-08-18T08:55:49Z")

</div>

I’ve seen this happen before. I _think_ it was caused by having a setup  
method declared in both test/test\_helper.rb and in your test class…  
but I  
could be wrong.

-Jonathan.
