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.
- 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:intest_department_company_defaults_to_parent’
- 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