cider-load-test / existence_validated Goto Github PK
View Code? Open in Web Editor NEWThis project forked from mhennemeyer/existence_validated
Mock belongs_to associations that are required via validates_existence_of
License: MIT License
This project forked from mhennemeyer/existence_validated
Mock belongs_to associations that are required via validates_existence_of
License: MIT License
= 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
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.