Skip to content

Random or Unique Seeds

Bret Pettichord edited this page Sep 30, 2015 · 2 revisions

Testers often need to generate test data that is unique to avoid collisions when testing in environments with lots of other test data. For example, let's assume that users in the system need to have unique email addresses. And let's also assume that we want to run our tests multiple times.

You can support these requirements with this subclass:

class SeededModel < Model
  key(:seed) { UUID.new.generate(:compact)[0..8] }}
end

And then it could be used like this:

class UserModel < SeededModel
  key(:first) { Faker::Name.first_name }
  key(:last) { Faker::Name.last_name }
  key(:email) { "#{first}.#{last}.#{seed}@devnull.com" }
end

Also, if for some reason, you then wanted to run your test code without this randomness, you could do this:

user = UserModel.new(seed: '12345678')
Clone this wiki locally