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

**URL:** <https://www.ruby-forum.com/t/unit-test-patterned-after-awdr-p-186-doesnt-work-as-expect/142640>\
**Category:** Rails\
**Created:** [July 10, 2008, 12:24am UTC](https://www.ruby-forum.com/t/unit-test-patterned-after-awdr-p-186-doesnt-work-as-expect/142640 "2008-07-10T00:24:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rainyglade](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/cab0a1/32.png) [@rainyglade](https://www.ruby-forum.com/u/rainyglade)\
**Post date:** [July 10, 2008, 12:24am UTC](https://www.ruby-forum.com/t/unit-test-patterned-after-awdr-p-186-doesnt-work-as-expect/142640/1 "2008-07-10T00:24:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rainyglade](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/cab0a1/32.png) [@rainyglade](https://www.ruby-forum.com/u/rainyglade)\
**Post date:** [July 10, 2008, 12:29am UTC](https://www.ruby-forum.com/t/unit-test-patterned-after-awdr-p-186-doesnt-work-as-expect/142640/2 "2008-07-10T00:29:28Z")

</div>

> 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

---

<div class="post-metadata">

**Author:** ![rainyglade](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/cab0a1/32.png) [@rainyglade](https://www.ruby-forum.com/u/rainyglade)\
**Post date:** [July 10, 2008, 1:04am UTC](https://www.ruby-forum.com/t/unit-test-patterned-after-awdr-p-186-doesnt-work-as-expect/142640/3 "2008-07-10T01:04:52Z")

</div>

Frederick C. 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!
