You would test it the same way you test anything that depends on IO, you don't make your function depend on a random number or an unpredictable value, the random number should be a function parameter.
In order to test something it must be made testable. Right now your function is not. Le mercredi 15 mars 2017 04:45:22 UTC+1, Doug Ireton a écrit : > > I'm a new Gopher and I'm working through "Learn Go" by Nathan Youngman > and trying to TDD the exercises to learn how to write testable Go code. > > I have a function to return a random spaceline > <https://play.golang.org/p/g5JnrIFyjo> from a string array. > > In Go, how do I test functions which depend on random numbers? And, yes, I > know that "math/rand" isn't truly random. > > Is it as simple as setting a seed right before I run my test, e.g. > rand.Seed(1)? Do I set rand.Seed(1) at the top of the _test.go file, or > at the beginning of each unit test? > > Also, am I seeding math.rand correctly in the Init() function? Will > seeding it in the Init() function override any seeding I do in my tests? > > My only other thought is to create an interface somehow to mock > rand.Intn(), but this seems like overkill and I don't know enough about > interfaces to know if this is inadvisable. > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.