Giter Site home page Giter Site logo

bishisht / object-factory Goto Github PK

View Code? Open in Web Editor NEW

This project forked from brightbox/object-factory

0.0 2.0 0.0 74 KB

A simple object factory that assists with creating valid model objects when writing specifications

Home Page: http://www.brightbox.co.uk/

Ruby 100.00%

object-factory's Introduction

Object-factory

A ruby gem designed to make it simple to create test objects within your test framework, so you don’t need to use nasty fixtures.

If you don’t use the a_saved method then it should work with any type of model, if you do use a_saved then it will work with any model that has a save method (that returns false on failure - so ActiveRecord and DataMapper should be OK).

Install

gem install object-factory

Usage

See the wiki page at github.com/brightbox/object-factory/wikis/home for the latest docs. In particular, wiki.github.com/brightbox/object-factory/usage-with-rails-rspec-and-cucumber is a pretty good example of how to get started.

However, it works something like this:

when_creating_a Person, :auto_generate => :employee_code

@person = a Person, :first_name => 'John', :last_name => 'Smith'
puts @person.employee_code # will show a unique auto-generated value

And your options are:

  • :auto_generate => [:field1, :field2]: generates a unique string value for the given field name or array of field names

  • :auto_confirm => :password: generates a unique string value for the given field name or array of field names and also sets field_name_confirmation to the same value

  • :generate_email_address => 📧 generates a randomised email address for the given field name or array of field names

  • :generate_ip_address => :ip: generates a randomised ip address for the given field name or array of field names

  • :set => { :field3 => ‘value3’, :field4 => ‘value4’ }: sets the given fields to the supplied static values

  • :generate => { :field5 => lambda { Date.today } }: sets the given fields to the supplied dynamic values

If you want to generate a unique value yourself, you can use Object::Factory’s next_number call (which has a simple shortcut a_number).

:generate => { :field => "unique-value-#{a_number}" }

Cleaning up

(currently only for ActiveRecord)

If you register your (ActiveRecord) class with the clean_up option, then the factory can ensure that all instances are deleted. This is useful if you cannot use transactional_fixtures (for example using Watir or Culerity in your test suite).

Add a clean_up parameter when registering your classes:

when_creating_a Person, :auto_generate => [:first_name, :last_name], :clean_up => true

Then in your “after” section call:

Object.factory.clean_up

This will call delete_all on any registered classes (hence it is currently ActiveRecord only).

Rails

To use this with rails, stick the following in your environment.rb:

config.gem "object-factory"

Also check out wiki.github.com/brightbox/object-factory/usage-with-rails-rspec-and-cucumber for more information on integrating Object-Factory with your application.

Released under the MIT Licence

Copyright © 2008 Brightbox Systems Ltd

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.

See www.brightbox.co.uk/ for contact details.

object-factory's People

Contributors

caius avatar gnufied avatar

Watchers

Bishisht Bhatta avatar James Cloos 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.