-
Notifications
You must be signed in to change notification settings - Fork 235
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Testing distribution::rand() #49
Comments
You can't mock static functions in PHPUnit; only object methods. If static method a uses static method b, you'd want to have unit tests on method b on its own. Then the tests on static method a are testing just a, since you know your tests on b test that that works properly. Now, if method b is a random number generator, then it is going to be impossible to test specific output since the output will depend on the rng. The best you could do is test that it is a number, that it is within the range of possible values based on the range of the rng, etc. This doesn't necessarily sound like an issue of not being able to mock static methods, but that a rng is being used. Even if there was just one static method and it used a PHP rand function, your output is dependent on that, and you can't know exactly what the output will be. |
Thinking about this a little more, a possibly elegant solution is to make your static method a higher-order function that accepts as an additional parameter a function (PHP callable closure) that will act as the random number generator. Then for unit tests, you can pass in your testRNG function that returns non-random numbers so you can test the expected output of your method knowing what the inputs will be because you seeded the RNG with your own values. Then, from a user interface perspective, you might want to wrap that higher-order function with a convenience wrapper method that will supply the standard RNG, but still give the user the option to directly call it with their own RNG if they want. Just an idea. |
I think I have a better solution. If I add the Dirac Delta as a distribution, I could add it's inverse function to the DiracDetla class, which would override the parent inverse function. The inverse of the Dirac Delta is Zero for all values of p. |
This is in a pull request. |
I don't know unit testing enough to mock it up correctly. I was thinking the test directory could have something like this, but it looks fishy the way it is. Any advice?
The text was updated successfully, but these errors were encountered: