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
create_table :members do |t|
t.column :name, :string
t.column :number, :string
The model is:
class Member < ActiveRecord::Base
The testing fixture is:
name: John S.
name: Joanne Jones
So if I write a little test case (yes I know hard coded values are
require File.dirname(FILE) + ‘/…/test_helper’
class ExampleTest < Test::Unit::TestCase
dup_member = Member.new(:name => ‘duplicate’, :number =>
assert !dup_member.save assert_equal 1, dup_member.errors.count assert_equal "has already been taken",
“assert_equal 1, dup_member.errors.count” will fail with:
<1> expected but was <2>.
The following line would also fail with:
<“has already been taken”> expected but was
<[“has already been taken”, “has already been taken”]>.
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?