Skip to content
Mock belongs_to associations that are required via validates_existence_of
Ruby JavaScript
Find file
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
lib
rails_root
spec
validates_existence
LICENCE
README
init.rb

README

= existence_validated

by Matthias Hennemeyer <mhennemeyer@gmail.com>

== Introduction

Rails Plugin. Depends on rspec/rails.
Mock belongs_to associations that are required via validates_existence_of.
A call to existence_validated(:assoc) yields the following:
* it returns {:assoc => assoc_moc, :assoc_id => assoc_mock.id}
* Assoc.exists? is stubbed and will return true
* Assoc.find(assoc_mock.id) will return assoc_mock

== Usage

Consider the following Model:

class Model < ActiveRecord::Base
  belongs_to :assoc
  validates_existence_of :assoc
end

If you mock the belongs_to association like this:
Model.create!(:assoc => mock_model(Assoc))
an error will be raised, because validates_existence 
verifies that the associated object really exists.

With existence_validated it works just fine:
Model.create!(existence_validated(:assoc))

It is also possible to pass an array of multiple required association identifiers:
Consider 

class OtherModel < ActiveRecord::Base
  belongs_to :assoc
  validates_existence_of :assoc
  
  belongs_to :other_assoc
  validates_existence_of :other_assoc
end

OtherModel.create!(existence_validated([:assoc, :other_assoc]))

=== I want to pass a specific mock object to my model!

No problem. existence_validated takes an optional hash where you can specify this:
Model.create!(existence_validated([:other_assoc, :assoc], :assoc => mock_model(Assoc)))

=== I want to set an association to nil 

Model.create!(existence_validated([:other_assoc, :assoc], :assoc => nil))
(raises)

=== Use Case
Consider the following valid_attributes helper method:
def valid_attributes(options={})
  assocs = [:user, :vendor]
  {
    :name => "Name",
    :price => 123
  }.update( existence_validated(assocs, options) ).update(options)
end

You can use this helper intuitional:

it "should require a user" do
  lambda {Model.create!(valid_attributes(:user => nil))}.should raise_error(/User/)
end
  


== INSTALL:

  $ ruby script/plugin install git://github.com/mhennemeyer/existence_validated.git


Copyright (c) 2008 Matthias Hennemeyer, released under the MIT license
Something went wrong with that request. Please try again.