Note that there are some explanatory texts on larger screens.

plurals
  1. POMaking multiple calls in unit test without using a for loop
    primarykey
    data
    text
    <p>Lets say I have a unit test similar to the below, is there a way to write one unit test rather than several but also avoid having a for loop in the unit test?</p> <pre><code>[Test] public void RunTestWithMultipleOptions() { MyClass code = new MyClass(); code.Prefix = "{DS1}"; //Options are {DS1}, {DS2}, {DS3}, {DS4} //Property could be set to //code.Prefix = "{DS1}{DS2}"; //code.Prefix = "{DS1}{DS2}{DS3}"; //And so on //Based on how many {DS} used a method needs calling code.InputDataStore(1,"Data1"); //If used {DS1}{DS2} in Prefix then //code.InputDataStore(1,"Data1"); //code.InputDataStore(2,"Data2"); //If used {DS1}{DS2}{DS3} in Prefix then //code.InputDataStore(1,"Data1"); //code.InputDataStore(2,"Data2"); //code.InputDataStore(3,"Data3"); string OutputData = String.Empty; code.Output += delegate(int Id, string Data) { if (Id == (int)OutputsEnum.OutputModified) OutputData = Data; }; //Call the input method which will raise the Output event which we can assert against code.Input("hi there"); //Assert that output has replace the prefix {DS} with the data in the datastorecontent list Assert.AreEqual("Data1hi there", OutputData); } </code></pre> <p>I can pass in the property value to the unit test method and use test cases but based on what the property is MyMethod needs to be called x number of times. Without putting a loop in the test I can't think of a way without having all the permetations as separate unit tests.</p> <p><strong>UPDATE:</strong> Here is the main contents of the class:</p> <pre><code> public event Action&lt;int, string&gt; Output; public string Prefix { get; set; } public string Postfix { get; set; } private List&lt;string&gt; DataStoreContents = new List&lt;string&gt;() { "", "", "", "" }; public void Input(string Data) { if (Output != null) { if (!String.IsNullOrEmpty(Prefix)) { Prefix = Prefix.Replace("{DS1}", DataStoreContents[0]); Prefix = Prefix.Replace("{DS2}", DataStoreContents[1]); Prefix = Prefix.Replace("{DS3}", DataStoreContents[2]); Prefix = Prefix.Replace("{DS4}", DataStoreContents[3]); } if (!String.IsNullOrEmpty(Postfix)) { Postfix = Postfix.Replace("{DS1}", DataStoreContents[0]); Postfix = Postfix.Replace("{DS2}", DataStoreContents[1]); Postfix = Postfix.Replace("{DS3}", DataStoreContents[2]); Postfix = Postfix.Replace("{DS4}", DataStoreContents[3]); } Output((int)OutputsEnum.OutputBeforeModified, Data); Output((int)OutputsEnum.OutputModified, Prefix + Data + Postfix); Output((int)OutputsEnum.OutputAfterModified, Data); } } } public void InputDataStore(int DataStore, string Data) { if (DataStore &lt; 1 || DataStore &gt; 4) throw new ArgumentOutOfRangeException("Datastore number out of range"); DataStoreContents[DataStore - 1] = Data; } } </code></pre> <p>I want to test that when I call <code>InputDataStore(1,"MyData1"); InputDataStore(2, "MyData");</code> that <code>Output</code> does in fact replace the relevant {DS1} values with the relevant string and also combine it with any other {DS} values</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload