I am trying to figure out how to have my method randomly select an account type from a drop down list. Basically in my web application the user will choose: active, inactive, prospect, or suspended from a drop down list. What I am trying to do is test this functionality and have my method randomly select a value when I run the test.
I am having a hard time referencing the method to the string value in the code if this makes sense. Any help would be great and if you need additional information let me know!
Here is what I have so far:
public void RandomStatusTypes()
{
List<string> statusTypes = new List<string> { "ACTIVE", "INACTIVE", "PROSPECT", "SUSPENDED" };
Random randStatus = new Random();
int index = randStatus.Next(0, 6);
string value = statusTypes[index];
}
The code will have a line saying StatusTyp = _Status, and hopefully I can assign a random status to the value so everytime I run the program it randomly picks a value.
Thank you!
Perhaps:
Don’t create the random instance in the method itself. Otherwise you will create the same statuses when the method is called very fast(f.e. in a loop). You should use a field(as shown above) or pass the random as argument to the method.
I’ve also moved the list outside of the method since it’s ineffective to create it always when it doesn’t change anyway. Last i’ve used
StatusTypes.Countto ensure that you always use a valid range even if you will change the list in future(f.e. add new statuses).