Strange Little Problem

qb
Wed, 26 Sep 2007 02:55:00 -0000

I just created a simple model and started to write unit tests for it
(I''ve cleaned out extraneous stuff).

The migration is:

class CreateMembers < ActiveRecord::Migration
  def self.up
    create_table :members do |t|
      t.column :name, :string
      t.column :number, :string
    end
  end

  def self.down
    drop_table :members
  end
end

The model is:

class Member < ActiveRecord::Base
  validates_uniqueness_of :number
end

The testing fixture is:

numeric_only_member:
  id: 1
  name: John Smith
  number: 15171914
alpha_numeric_member:
  id: 2
  name: Joanne Jones
  number: wke1234

So if I write a little test case (yes I know hard coded values are
fragile):

require File.dirname(__FILE__) + ''/../test_helper''
require ''member''

class ExampleTest < Test::Unit::TestCase
  fixtures :members

  def test_uniqueness_of_member
    dup_member = Member.new(:name => ''duplicate'', :number
=>
''15171914'')

    assert !dup_member.save
    assert_equal 1, dup_member.errors.count
    assert_equal "has already been taken",
dup_member.errors.on(:number)
  end
end

Problem:

"assert_equal 1, dup_member.errors.count" will fail with:
test_uniqueness_of_member(SimpleTest) [test/unit/simple_test.rb:11]:
<1> expected but was <2>.

The following line would also fail with:
test_uniqueness_of_member(SimpleTest) [test/unit/simple_test.rb:12]:
<"has already been taken"> expected but was
<["has already been taken", "has already been
taken"]>.

Solution:

If I remove " require ''member'' " from the test case
then everything
passes. I have no idea why. (Yes I''m a newbie, please don''t
laugh and
point). I''m digging through ActiveRecord, UnitTest, and Ruby docs and
source but haven''t found an answer yet. Any ideas?

Thanks...qb