Creating Numbers With AutoFixture by Mark Seemann
Previously, we saw how AutoFixture creates strings. In this post, I'll explain how it creates numbers. Once again, the algorithm that I'll explain here is the default algorithm, but if you don't like it, you can replace it with something else.
It's very simple: Numbers are returned in the ordered sequence of natural numbers (1, 2, 3, 4, 5, …). The first time you call
the returned number will be 1, the second time 2, etc.
The reason I chose that particular algorithm is because it creates numbers that we, as humans, find… well… natural!
A lot of the domains we model work with natural numbers, and even if you write an API where negative numbers are allowed, it's fairly unlikely that positive numbers will not be allowed. Thus, in most cases, small positive integers tend to be ‘nice' values in most APIs - and recall that when we do TDD, we focus on the Happy Path, so it's important to pick values that take us down that path.
Using the overload that takes a seed, like this:
has no effect - the seed (in this case 42) is simply ignored, so if you call this after first calling the parameterless overload twice, the return number is going to be 3.
Each number type, however, has its own sequence, so even if you've been creating a few Int32 instances like above,
will return 1.
The following number types all work that way: