Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jcf/stub_factory
A really simple Rspec helper that builds instances with stubs
https://github.com/jcf/stub_factory
Last synced: 1 day ago
JSON representation
A really simple Rspec helper that builds instances with stubs
- Host: GitHub
- URL: https://github.com/jcf/stub_factory
- Owner: jcf
- License: mit
- Created: 2010-08-31T14:42:00.000Z (about 14 years ago)
- Default Branch: master
- Last Pushed: 2010-09-15T11:53:54.000Z (about 14 years ago)
- Last Synced: 2024-10-31T14:54:02.371Z (14 days ago)
- Language: Ruby
- Homepage: http://github.com/jcf/stub_factory
- Size: 102 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# stub_factory
This is just something I was working on for fun a while back and decided I'd
push it to Github.Rspec provides `subject` which will do most of what this code does. You
probably want to use that instead.## Installation
gem install stub_factory
Or you can \[fork and\] clone the repo and install it using Jeweler's `rake
install` task.## Usage
In your spec_helper
require 'stub_factory'
# Optionally include StubFactoryIn your specs
factory(NextBigThing::Ninja::Foo)
describe 'awesomeness' do
it 'should be easy' do
foo(:stub => 'this').should be_a?(NextBigThing::Ninja::Foo)
end
endYou can also specify the method name you want to use to access your instance
factory(NextBigThing::Ninja::Foo, :method_name => :ninja_foo)
describe 'awesomeness' do
it 'should be easy' do
# Then you can access your instance using your preferred method name
ninja_foo(:stub => 'this').should be_a?(NextBigThing::Ninja::Foo)
end
endCheck out
[stub_factory_spec.rb](http://github.com/jcf/stub_factory/blob/master/spec/stub_factory_spec.rb)
to see `StubFactory` in use.## Alternative Usage
If you already use FactoryGirl, as I do on a lot of my projects you may find the
following method useful as it allows you to setup similar persistent instances
of your Factories. Simple paste it in to your `spec_helper.rb`def factory(factory_name, options = {})
method_name = (options[:method_name] || factory_name).to_s.to_symdefine_method(method_name) do |*args|
var_name = "@#{method_name}"
value = instance_variable_get(var_name)
return value if valueattributes = args.shift || {}
stubs = args.shift || {}super_mock = Proc.new { |factory_name, attributes, stubs|
Factory(factory_name, attributes).tap do |super_mock|
super_mock.stub!(stubs)
end
}instance_variable_set(var_name, super_mock.call(factory_name, attributes, stubs))
end
endThe difference being you pass the factory name to the `factory` method instead of
a class and FactoryGirl does everything else.factory(:post)
describe 'something' do
it 'has attributes' do
post(:published_at => Time.now)
end
end# calls Factory(:post, :published_at => Time.now)
## Note on Patches/Pull Requests
* Fork the project.
* Make your feature addition or bug fix.
* Add tests for it. This is important so I don't break it in a
future version unintentionally.
* Commit, do not mess with rakefile, version, or history.
(if you want to have your own version, that is fine but bump version in a
commit by itself I can ignore when I pull)
* Send me a pull request. Bonus points for topic branches.## Copyright
Copyright (c) 2010 James Conroy-Finn. See LICENSE for details.