GithubHelp home page GithubHelp logo

cider-load-test / existence_validated Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mhennemeyer/existence_validated

0.0 0.0 0.0 200 KB

Mock belongs_to associations that are required via validates_existence_of

License: MIT License

JavaScript 0.54% Ruby 99.46%

existence_validated's Introduction

= existence_validated

by Matthias Hennemeyer <[email protected]>

== 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

existence_validated's People

Contributors

mhennemeyer avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.