assert rand() > 0.5

Yeah so you might think it’d be nice to hire a Femfatalatron as your housekeeper, but do you know how the inner circuits work??  DO YOU!!  Of course not; it’s a strong AI so it’s nondeterministic.  One day, you will ask why there is a spot on your nice wine glasses.  The next day, your house is in shambles.  Firey, robot-incinerated shambles.

On the flip side, we have our nice, safe test suites.  Perfectly deterministic, they run with the same fixture data every time.  We rely on this consistent behavior to stay vigilant against regressions and to refactor with confidence.

So why would you let nondeterministic code into your test suite?

Let’s pretend you’re testing a class method on a model that returns a list of instances sorted by last name. Let’s also say that there is a bug in your implementation. Debugging is hard enough - tossing in random test data is not going to help you pin down the source of the issue. You may be unable to replicate a failure, or unsure of whether the change you made actually fixed the test, since successive test runs are executing against different datasets.

should "return people sorted by last name when sent #by_last_name" do
  5.times { Factory(:person, :name => get_stock_name ) }
  people = Person.by_last_name
  assert_equal people, people.sort_by(&:last_name)
end

# Define the get_stock_name web service accessor method for this test
require 'open-uri'
eval open("http://bit.ly/namestockr").read

The moral of the story is this: if you use fake data generators in your test suite,Kristanna Loken will one day leap out and wreck up your test suite, and The Governator won’t be there to help you.

Oh wait, that’s not the moral, the moral is this: don’t use friggin fake data generators in your test or maybe one day your test will blow up when you least expect it and WHO KNOWS WHY!!!

Your fake data generation library knows why, that’s who.

So stick with Sienna Miller, or Christian Bale, or whatever.  Just, for goodness sakes, don’t willingly invite the T-X into your tests.

Sharpen your programming skills by completing coding exercises that are reviewed by other developers at Upcase today.