Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/brentgreeff/hash_factory
Light-weight Rails test factory
https://github.com/brentgreeff/hash_factory
automated-testing rails
Last synced: about 7 hours ago
JSON representation
Light-weight Rails test factory
- Host: GitHub
- URL: https://github.com/brentgreeff/hash_factory
- Owner: brentgreeff
- License: mit
- Created: 2009-12-06T04:11:39.000Z (almost 15 years ago)
- Default Branch: master
- Last Pushed: 2011-01-13T10:26:17.000Z (almost 14 years ago)
- Last Synced: 2024-10-20T20:30:08.137Z (about 1 month ago)
- Topics: automated-testing, rails
- Language: Ruby
- Homepage: http://www.brentgreeff.com/rails-plugins/hash-factory
- Size: 97.7 KB
- Stars: 1
- Watchers: 4
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.rdoc
- License: MIT-LICENSE
Awesome Lists containing this project
README
= HashFactory
== Light-weight Rails test factory
== Install
gem install hash_factory
Add this line to the top of test/test_helper.rb
require 'test_help' # this should already be here
HashFactory.setup== Lets start with the calling code
I like to setup my test data in the test.
* Along the lines of
test "A User with a blank last name should be invalid" do
@user = create_user :last_name => ''
assert @user.invalid?
end== A few things to notice:
No Factory calls, these methods are global to the test,
do I really need the namespace, will I ever need a SomethingElse.create_userI define the arguments relevant to the test explicitly.
I dont need to look at the implementation to see if the user has a last_name.I assume the factory handles the rest and passes me a 'VALID' instance.
== Test things in isolation
A factory that returns an invalid instance will eventually cause a failure that ripples
through your test code.== How to use it
* create a factories directory:
mkdir test/factoriesAdd a module to this folder and the methods
automatically become available in your tests.* Mine tend to look like this:
module UserFactory
def create_user(options={})
build :user, user_options(options)
end
def user_options(options={})
{
:first_name => 'Jim',
:last_name => 'Bean',
:email => "[email protected]",
:account => options[:account] || create_account
}.merge(options)
end
endAdd as many or as few files as you like.
The advantage of using this plugin is you can build your objects any way you like.
Don't worry about protected attributes
the 'build' method handles it for you.=== Pluralization Issues
Be sure to update your 'initializers/inflections.rb'
If you get an Constant missing error when loading a factory.=== Factory love in the console.
Call
require 'hash_factory'
HashFactory.load_in(self)Or event better
copy the file:
config/initializers/factory_in_console.rb
and it's always there waiting for you.
Copyright (c) 2009 [Brent Greeff], released under the MIT license