Unit test patterned after AWDR p. 186 doesn't work as expect

I have the following Ruby migration:

class CreateSimulations < ActiveRecord::Migration   def self.up     create_table :simulations do |t|         t.string :name, :desc, :time_units         t.column :run_length, :float

      t.timestamps     end   end

  def self.down     drop_table :simulations   end end

My class has the following validations:

class Simulation < ActiveRecord::Base

   validates_uniqueness_of :name     validates_numericality_of :run_length     validates_inclusion_of :time_units,                             :in => %w{sec min hr days},                             :message => "should be 'sec', 'min', 'hr', 'days'"     validates_presence_of :name, :time_units, :run_length

end

My unit test looks like this:

def test_unique_name     sim1 = Simulation.new( :name => "base", :run_length => 84600, :time_units =>"sec")     assert sim1.save

    sim2 = Simulation.new     sim2.name ="base"     sim2.run_length = 8     sim2.time_units = "hr"     assert !sim2.save, sim2.errors.on(:name)     assert_equal ActiveRecord::Errors.default_error_messages[:taken],sim2.errors.on(:name)   end

It fails on the assert_equal line with the following info:

  1) Failure: test_unique_name(SimulationTest)     [test/unit/simulation_test.rb:61:in `test_unique_name'      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']: already taken. <"has already been taken"> expected but was <["has already been taken", "has already been taken"]>.

4 tests, 17 assertions, 1 failures, 0 errors

Why are there 2 copies of the "has already been taken" error message? A similar test with a different object works just fine.

Thanks, Louise

Why are there 2 copies of the "has already been taken" error message? A similar test with a different object works just fine.

Random guess: somewhere in your app or in its tests you've got a "require 'simulation'" that is loading simulation.rb a second time.

Fred

Frederick Cheung wrote:

Why are there 2 copies of the "has already been taken" error message? �A similar test with a different object works just fine.

Random guess: somewhere in your app or in its tests you've got a "require 'simulation'" that is loading simulation.rb a second time.

Fred

Good guess, that was exactly the problem.

Thanks!