Skip to content
Browse files

Fabrication adapter

  • Loading branch information...
1 parent 7f4c017 commit 50705b2fb04e1e82d01381f2701360d5fe37e0e7 @dabit dabit committed with
Showing with 69 additions and 3 deletions.
  1. +26 −0 lib/pickle/adapter.rb
  2. +2 −1 pickle.gemspec
  3. +41 −2 spec/pickle/adapter_spec.rb
View
26 lib/pickle/adapter.rb
@@ -132,6 +132,32 @@ def create(attrs = {})
end
end
+ # fabrication adapter
+ class Fabrication < Adapter
+ def self.factories
+ if defined? ::Fabrication
+ ::Fabrication::Support.find_definitions if ::Fabrication::Fabricator.schematics.empty?
+ factories = []
+ ::Fabrication::Fabricator.schematics.each {|v| factories << new(v)}
+ factories
+ end
+ end
+
+ def initialize(factory)
+ if defined? ::Fabrication
+ @klass, @name = factory[1].klass, factory[0].to_s
+ end
+ end
+
+ def create(attrs = {})
+ if defined? ::Fabrication
+ ::Fabrication::Fabricator.generate(@name.to_sym, {
+ :save => true
+ }, attrs)
+ end
+ end
+ end
+
# ORM adapter. If you have no factory adapter, you can use this adapter to
# use your orm as 'factory' - ie create objects
class Orm < Adapter
View
3 pickle.gemspec
@@ -29,9 +29,10 @@ Gem::Specification.new do |s|
s.add_development_dependency "rails", "~>3.1.0"
s.add_development_dependency "cucumber-rails"
s.add_development_dependency "factory_girl"
+ s.add_development_dependency "fabrication"
s.add_development_dependency "machinist"
s.add_development_dependency "rcov"
s.add_development_dependency "database_cleaner"
s.add_development_dependency "capybara"
s.add_development_dependency "sqlite3-ruby"
-end
+end
View
43 spec/pickle/adapter_spec.rb
@@ -2,6 +2,7 @@
require 'active_record'
require 'factory_girl'
+require 'fabrication'
require 'machinist/active_record'
require 'pickle/adapters/active_record'
@@ -72,7 +73,7 @@
describe 'FactoryGirl' do
before do
Pickle::Adapter::FactoryGirl.stub!(:model_classes).and_return([@klass1, @klass2, @klass3])
-
+
if defined? ::FactoryGirl
@orig_factories = ::FactoryGirl.factories.dup
::FactoryGirl.factories.clear
@@ -125,7 +126,45 @@
end
end
end
-
+
+ describe 'Fabrication' do
+ before do
+ @schematic1 = [:one, Fabrication::Schematic.new(@klass1)]
+ @schematic2 = [:two, Fabrication::Schematic.new(@klass2)]
+ ::Fabrication::Fabricator.stub(:schematics).and_return([@schematic1, @schematic2])
+ end
+
+ it '.factories should create one for each fabricator' do
+ Pickle::Adapter::Fabrication.should_receive(:new).with(@schematic1)
+ Pickle::Adapter::Fabrication.should_receive(:new).with(@schematic2)
+
+ Pickle::Adapter::Fabrication.factories
+ end
+
+ describe ".new" do
+ before do
+ @factory = Pickle::Adapter::Fabrication.new(@schematic1)
+ end
+
+ it ".new should have name of schematic name" do
+ @factory.name.should == 'one'
+ end
+
+ it "should have klass of build_class" do
+ @factory.klass.should == @klass1
+ end
+ end
+
+ describe ".create" do
+ it "returns the fabricated instance" do
+ @factory = Pickle::Adapter::Fabrication.new(@schematic1)
+ ::Fabrication::Fabricator.should_receive(:generate).
+ with(@factory.name.to_sym, {:save => true}, {:attribute => 'value'})
+ @factory.create({:attribute => 'value'})
+ end
+ end
+ end
+
describe 'Machinist' do
before do
Pickle::Adapter::Machinist.stub!(:model_classes).and_return([@klass1, @klass2, @klass3])

0 comments on commit 50705b2

Please sign in to comment.
Something went wrong with that request. Please try again.