You are here: Browse Railsplugins Test Exemplars
This plugin is based off of Piers Cawley’s post at http://www.bofh.org.uk/articles/2007/08/05/doing-the-fixture-thing. It allows you to easily create valid ActiveRecord objects for use in testing.
Usage is really simple. In your test_helper.rb or spec_helper.rb file just require “test_exemplars” include ExemplarBuilder
This provides you with the exemplify method that you can use to create an exemplar for any AR class.
Your best bet is to take a look at spec/test_exemplars_spec.rb to grok the usage. Here are a couple examples though. Assume the following ActiveRecord class:
class Chicken < ActiveRecord::Base
attr_protected :ssn
end
exemplify also takes an optional block. This is useful for methods you need to call on the exemplar. For example, attr_protected attributes can’t be mass-assigned, you need to explicitly set them. Of course you can use it for any object initialization that isn’t basic hash attributes.
Finally, you can automatically populate a field’s value with an autoincrementing exemplar ID.
You can also automatically save the record with ARClass.create_exemplar and ARClass.create_exemplar!. They’re just convenience methods that build an exemplar and call #save and #save! respectively.
NOTE: This description has been extracted from the Plugin README and so the formatting may need updating to make browser friendly