Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Call factory with the factory name and an optional method_name
- #
- # factory(:user)
- # factory(:expensive_product, :method_name => :product)
- #
- # Then in all of your it/specify blocks you can call upon a a super-mocked,
- # majorly-stubbed Factory.
- #
- # describe "Something awesome"
- # factory(:long_factory_name_for_user_model, :method_name => :user)
- #
- # it "should do something" do
- # user(:attribute => 'James').attribute.should == 'James'
- # end
- #
- # it "should do more than just that" do
- # u = user({:attribute => 'James'}, {:stub => 'a result'})
- # u.stub should == 'a result'
- # end
- # end
- def factory(factory_name, options = {})
- method_name = (options[:method_name] || factory_name).to_s.to_sym
- define_method(method_name) do |*args|
- var_name = "@#{method_name}"
- value = instance_variable_get(var_name)
- return value if value
- attributes = 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
- end
Add Comment
Please, Sign In to add comment