Posted By

jnunemaker on 10/28/08


Tagged

testing rspec fixtures factories


Versions (?)

Who likes this?

6 people have marked this snippet as a favorite

inkdeep
millisami
wdso
miyazima
webstic
madaemo


factory girl example


 / Published in: Ruby
 

  1. #### spec_factory.rb
  2. require 'factory_girl'
  3.  
  4. Factory.sequence :login do |n|
  5. "bob#{n}"
  6. end
  7.  
  8. Factory.sequence :email do |n|
  9. "person#{n}@example.com"
  10. end
  11.  
  12. Factory.sequence :subdomain do |n|
  13. "joe#{n}"
  14. end
  15.  
  16. Factory.sequence :checksum do |n|
  17. n
  18. end
  19.  
  20. Factory.define :user do |u|
  21. u.login { |l| l.login = Factory.next(:login) }
  22. u.password "tester1"
  23. u.password_confirmation "tester1"
  24. u.email { |e| e.email = Factory.next(:email) }
  25. end
  26.  
  27. Factory.define :blog do |b|
  28. b.title "Joe's Blog"
  29. b.subdomain { |s| s.subdomain = Factory.next(:subdomain) }
  30. b.association :owner, :factory => :user
  31. b.association :design
  32. end
  33.  
  34. Factory.define :post do |p|
  35. p.title "First post!"
  36. p.body "Woot, my blog is sexy!"
  37. p.association :blog
  38. p.association :creator, :factory => :user
  39. end
  40.  
  41.  
  42. # post_spec.rb
  43.  
  44. describe Post do
  45. before(:each) do
  46. user = Factory(:user)
  47. blog = Factory(:blog, :owner => user)
  48.  
  49. @post = Factory(:post, :creator => user, :blog => blog)
  50. end
  51.  
  52. describe "validations" do
  53.  
  54. it "should be valid" do
  55. @post.should be_valid
  56. end
  57. end
  58. end

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: praveendiangycom on July 29, 2009

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.

You need to login to post a comment.