Rails Model Test

Given this model:

class Organization < ActiveRecord::Base
has_many :users

validates :name, :presence => true
validates :subdomain, :presence => true
validate :has_at_least_one_user

private
def has_at_least_one_user
errors.add(:users, “Must have at least one user.”) unless
users.length > 0
end
end

And this set of examples:

require ‘spec_helper’

describe Organization do
before do
@organization = Organization.new(name:“an org”, subdomain:“anorg”)
end

it ‘is valid with valid attributes’ do
@organization.users << mock_model(“User”)
@organization.should be_valid
end
it ‘is not valid without an organization name’ do
@organization.name = nil
@organization.should_not be_valid
end
it ‘is not valid without a subdomain’ do
@organization.subdomain = nil
@organization.should_not be_valid
end
it ‘is not valid if subdomain is not unique’
it ‘is not valid without at least one user’ do
@organization.users = []
@organization.should_not be_valid
end
end

I get this failure:

Failures:

  1. Organization is valid with valid attributes
    Failure/Error: @organization.users << mock_model(“User”)
    Mock “User_1001” received unexpected message :to_ary with (no args)

    ./spec/models/organization_spec.rb:9:in `block (2 levels) in <top

(required)>’

Question 1: Why can’t I seem to add a mock User this way?

Question 2: Are there any better ways to test/implement a model that
requires at least one association?

Thanks!

Matt Smith