Adding Dynamic Versions to the Policy Factory

FactoryGirl provides a lot of flexibility when it comes to creating associations, so following their example, TOSBack is using an after(:create) callback to produce some content in the tests that appears to be changing over time:

factory :policy_with_sites_and_versions do
  ignore do
    sites_count 5
    versions_count 5
  end

  after(:create) do |policy, eval|
    eval.sites_count.times { policy.sites << FactoryGirl.create(:site) }
    eval.versions_count.times do |n| 
      policy.versions << FactoryGirl.create(:version, policy: policy, previous_crawl: policy.detail[0..-(n+2)], created_at: (n+1).days.ago)

      # i.e. older the created_at date in the db, the shorter the version is. 
      # e.g. two days old = two characters sliced off.
      # It appears in tests like one character is added per day
    end
  end
end #with_sites

Then, it’s as simple as creating the factory with a “versions_count” attribute:

describe "changes_from_previous()" do
  # policy model has a callback that creates a version 'current version' so
  # @policy really has 4 versions below even though versions_count is 3
  # see factories/policies.rb for details on differences between versions
  let(:vcount) { 3 }

  before(:each) { @policy = FactoryGirl.create(:policy_with_sites_and_versions, sites_count: 1, versions_count: vcount) }

You can see the entire factory file for policies on github.

Verifying the generated test data

I added the following “test” to my version_spec.rb file right after the creation of the factory to make sure everything was working as expected:

it { @policy.versions.each {|v| puts "#{v.created_at}\t#{v.previous_crawl}\n\n"} }

Then, running rspec prints the state of the versions at that point during the tests! (edited to fit)

2013-03-13 21:36:05 UTC	Current Version

2013-03-12 21:36:05 UTC	 <p>500px is founded on the principle of helping ...
    We know that you care about how your personal information is used and...
    By visiting the 500px website, you are accepting the practices outlin...
    <p>This Privacy Policy covers 500px's treatment of personal informati...
    This policy does not apply to the practices of third parties that 500...
    <br> Information Collected by 500px <p>We only collect personal infor...
    This information allows us to provide you with a customized and effic...
    We collect the following types of information from our 500px users:<b...
    <br>

2013-03-11 21:36:05 UTC	 <p>500px is founded on the principle of helping ...
    We know that you care about how your personal information is used and...
    By visiting the 500px website, you are accepting the practices outlin...
    <p>This Privacy Policy covers 500px's treatment of personal informati...
    This policy does not apply to the practices of third parties that 500...
    <br> Information Collected by 500px <p>We only collect personal infor...
    This information allows us to provide you with a customized and effic...
    We collect the following types of information from our 500px users:<b...
    <br

2013-03-10 21:36:05 UTC	 <p>500px is founded on the principle of helping ...
    We know that you care about how your personal information is used and...
    By visiting the 500px website, you are accepting the practices outlin...
    <p>This Privacy Policy covers 500px's treatment of personal informati...
    This policy does not apply to the practices of third parties that 500...
    <br> Information Collected by 500px <p>We only collect personal infor...
    This information allows us to provide you with a customized and effic...
    We collect the following types of information from our 500px users:<b...
    <b

…and it passes (with a cryptic name) :P

example at ./spec/models/version_spec.rb:40

Notice that it loses a character for each day older it is in the database. This gives us dynamic, predictable policy versions to use when testing!

Leave a Reply

  • (will not be published)

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>