factory girl example


/ Published in: Ruby
Save to your folder(s)



Copy this code and paste it in your HTML
  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

You need to login to post a comment.