---
title: assert rand() > 0.5
teaser:
tags: web,testing
author: Jason Morrison
published_on: 2009-08-25
---

Yeah so you might think it'd be nice to hire a
[Femfatalatron](http://bit.ly/cyberiad-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](http://xunitpatterns.com/test%20fixture%20-%20xUnit.html) 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?

![''](http://www.watchmoviestreaming.com/pictures/terminator31.jpg)

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.

```ruby
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](http://www.imdb.com/title/tt0181852/) will one day leap
out and wreck up your test suite, and [The
Governator](http://thebsreport.files.wordpress.com/2009/02/arnold_governator.jpg?w=400&amp;h=663)
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](http://www.imdb.com/title/tt0432402/), or
[Christian Bale](http://www.imdb.com/title/tt0361862/), or whatever.  Just, for
goodness sakes, don't willingly invite the T-X into your tests.
