c# - looping in unit test bad? -


I have a unit test which depends on the random dice roll I roll to 20 sided die and if the value 20 is counted as an important hit.

The one I am doing now is rolling 20 side rolls up to 300 times. If one of those rolls is 20 then I know that I am an important hit.

This code looks like this:

  Public class dicril {public full value {receipt; Set; } Public bool ethical {get; Set; } // Here is the code, which is set to "true" at 20}, if it determines the truth, then it determines the truth [test] Public Zero DiceCanRollCriticalStrikes () {bool IsSuccessful = false; Dicrrol dice roll = new dicirol (); For (int i = 0; i <300; i ++) {diceRoll.Value = Dice.Roll (1, 20); // Rolls once die 20 times once (diceRoll.Value == 20 & amp; amp; amp; amp;; DiceRoll.IsCritical) {IsSuccessful = true; break; }} If (ezusufible) // passed the exam and // test failed}  

Although the test does exactly what I want I can not help but I think That's something I'm doing wrong.

On the related note, there is other information about this in the Dias Roll class but my question is specifically about looping in a unit test, so I left it to make it more clear The problem with this approach is that you are relying on a random behavior. There is a possibility that within the 300 rolls, the desired state never appears, and without testing the code incorrect, the unit test fails.

I try to remove the dice roll log from the dice class through an interface (for example "IDiceRoller") then you can apply random dice roller in your application, and your unit Another dice roller can be implemented in the testing project. This one can always return the predefined value. In this way you can write test for specific dice values ​​without expecting to show looping and value.

Example:

(code in your application)

  Public Interface IDiceRoller {int GetValue (int lowerBound, int upper bound); } Public class DefaultRoller: IDiceRoller {public int GetValue (int lower bound, int upper bound) {// low value of bore and upper bound between random value}} public square dies {Private Static IDCer Roller _diceRoller = new DefaultRoller (); Public Fixed Zero SetDiceRoller (IDiceRoller diceRoller) {_diceRoller = diceRoller; } Public static zero roll (int lower bound, int upper bound) {int newValue = _diceRoller.GetValue (low bound, upper bound); // use newValue}}  

... and in your unit test project:

  Internal category MockedDiceRoller: IDiceRoller {public int value {get; Set; } Public int GetValue (Int lower bound, int upper bound) {return this.Value; }} Now, in your unit test, you can create a  MockedDiceRoller , you can set the price you want to dice, persecuted in the set. Set the dice roller  dice  class, roll and verify that behavior: 

  MockedDiceRoller diceRoller = New MockedDiceRoller (); Dissolor.value = 20; Dice.SetDiceRoller (diceRoller); Dice Roll (1, 20); Assert.IsTrue (Dice.IsCritical);  

Comments

Popular posts from this blog

oracle - The fastest way to check if some records in a database table? -

php - multilevel menu with multilevel array -

jQuery UI: Datepicker month format -