/ Published in: Ruby
Expand |
Embed | Plain Text
#### spec_factory.rb require 'factory_girl' Factory.sequence :login do |n| "bob#{n}" end Factory.sequence :email do |n| "person#{n}@example.com" end Factory.sequence :subdomain do |n| "joe#{n}" end Factory.sequence :checksum do |n| n end Factory.define :user do |u| u.login { |l| l.login = Factory.next(:login) } u.password "tester1" u.password_confirmation "tester1" u.email { |e| e.email = Factory.next(:email) } end Factory.define :blog do |b| b.title "Joe's Blog" b.subdomain { |s| s.subdomain = Factory.next(:subdomain) } b.association :owner, :factory => :user b.association :design end Factory.define :post do |p| p.title "First post!" p.body "Woot, my blog is sexy!" p.association :blog p.association :creator, :factory => :user end # post_spec.rb describe Post do before(:each) do user = Factory(:user) blog = Factory(:blog, :owner => user) @post = Factory(:post, :creator => user, :blog => blog) end describe "validations" do it "should be valid" do @post.should be_valid end end end
Comments
Subscribe to comments
You need to login to post a comment.

I really wish you had posted the definitions of Post, Blog and User classes. That would have made this example complete. I'm still struggling with my Factory girl setup, though this snippet helped.