Yet Another ObjectMother pattern implementation for rails testing
We meet some problems with factory-girl:
-
We require quite complex logic for creation of test models
-
We require use factories with running rails application for integration tests
-
So why don’t make factory just another class in rails lib, and get extensibility of factories/mothers and rails lazy loading and dependency management?
To define factory/mother for model User just create class in your lib directory:
#lib/famili/user.rb` module Famili class User < Mother fist_name { 'nicola' } last_name { 'nicola' } email { "#{last_name}@mail.lv" } def before_save(user) #... end def after_create(user) #... end end end #lib/famili/article module Famili class Article < Mother #creating association user { Famili::User.create } title { "article by #{user.last_name}" } end end
And u can use it anywhere in tests or controllers:
Famili::User.create(:fist_name=>'Override') # create model Famili::User.build(:fist_name=>'Override') # build model not saving Famili::User.hash(:fist_name=>'Override') #get attributes hash Famili::Article.create #create article with user
U can inherite mothers just like plain ruby classes, Just think each declaration field_name {…} as method definition
module Famili class User < Mother name { "nicola" } end class Person < User email { "#{name}@emial.com" } end end
Mother have some usable methods, which can be used
module Famili class User < Mother last_name { 'nicola' } login { "#{last_name}_#{unique}" } number { sequence_number } end end
-
sequence_number - incremented with each instance
-
unique - just unique string
-
we a planing add more
each declaration
field_name { block }
just create method
def field_name end
and register attribuite to copy in model
field :field_name
in config/environment.rb:
config.gem 'famili'
and then
sudo rake gems:install
-
0.0.1 - created
-
0.0.2 - add inheritance, and mother methods [unique,sequence_number]
-
0.0.3 - fix Mother.create call model.save!; Mother.hash return symbolized hash
-
0.0.5 - add raise NoMethodError if property declared without block (becose it is error-prone), fix Famili::Mother.class#name method
-
0.0.6 - rename Mother#hash to Mother#build_hash (to avoid conflicts with Object#hash in Ruby 1.9). Old name keeped as alias when using with oldest versions of Ruby for backward compatibility.
-
generators
(The MIT License)
Copyright © 2010 niquola
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.