31 Jul '12, 6pm

Want to use FactoryGirl in some interesting ways? Read about mind-bending factories:

July 31, 2012 Tagged: factory girl ruby rails factories testing Comments (View) Mind-Bending Factories People often forget that FactoryGirl is just using Ruby to instantiate objects. Most of the time, yes, you’ll use FactoryGirl in conjunction with your favorite ORM to instantiate objects for testing Rails apps. Want a simplistic stub? FactoryGirl.define do factory :tiny_stub, class: OpenStruct end # >> user = FactoryGirl.build(:tiny_stub, admin?: true, name: 'John Doe') # => #<OpenStruct admin?=true, name="John Doe"> # >> user.admin? # => true # >> user.name # => "John Doe" Let’s go a bit crazy: what about creating a factory that generates URLs? Writing out valid string URLs is a pain and copy/paste seems both problematic and error-prone. If I want to change the protocol to https:// or switch a subdomain, it gets rougher. Throw in ports and paths and you’re itching for a ...

Full article: http://robots.thoughtbot.com/post/28146418628/mind-bendin...

Tweets

Love this trolling from inside the company:

robots.thoughtbot.com 03 Aug '12, 3pm

Josh Clayton recently posted about Mind Bending Factories — demonstrating how a 20+ line factory definition lets you use F...