I have the following STI table:
def self.up
create_table :distributions do |t|
t.string :type
t.integer :simulation_id
t.string :dist_name
t.string :desc, :default=> ‘fixed’
t.float :param1, :default => 5.0
t.float :param2, :param3
t.timestamps
#fields for RscDist
t.integer :resource_id
#fields for LoadDist
t.integer :load_id
end
end
My model file looks like this:
class Distribution < ActiveRecord::Base
validates_presence_of :desc, :param1
validates_uniqueness_of :dist_name, :scope => "simulation_id"
validates_inclusion_of :desc,
:in => %w{fixed exponential normal uniform
triangular},
:message => “should be ‘fixed’, ‘exponential’,
‘normal’, ‘uniform’, ‘triangular’”
validates_numericality_of :param1, :param2, :param3
def to_s
"desc = " + desc + " param1 = " + param1.to_s + " dist_name = "
-
dist_name + " type = " + type.to_s
endprotected
def validate
#desc can be fixed, exponential, normal, uniform, triangular
errors.add(“need 2nd parameter”) if desc == ‘normal’ || desc ==
‘uniform’ || desc == ‘triangular’
errors.add(“need 3rd parameter”) if desc == ‘triangular’
end
end
class LoadDist < Distribution
validates_uniqueness_of :dist_name, :scope => "load_id"
belongs_to :load
end
class RscDist < Distribution
validates_uniqueness_of :dist_name, :scope => “resource_id”
belongs_to :resource
end
In my unit tests I can’t seem to get a valid object. Here’s my setup:
def setup
@s1 = simulations(:base)
@rsc1 = resources(:rsc1)
@d1 = RscDist.new
@d1.simulation_id = @s1.id
@d1.resource_id = @rsc1.id
end
and then the test:
def test_params_exp
@d1.desc = 'exponential'
assert [email protected]?
@d1.param1 = 14
@d1.dist_name = 'dist1'
puts @d1.to_s
assert @d1.valid?
end
The final line “assert @d1.valid?” fails, even though I have a :desc
field that is properly spelled, a numerical :param1, and a unique
:dist_name. The message is:
- Failure:
test_params_exp(DistributionTest)
[test/unit/distribution_test.rb:35:intest_params_exp' c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.1.0/lib/active_support/testi ng/setup_and_teardown.rb:33:in
send’
c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.1.0/lib/active_support/testi
ng/setup_and_teardown.rb:33:in `run’]:
is not true.
What am I doing wrong?