Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to unit test a switch statement with a dependency on Console
    text
    copied!<p>I'm brand new to unit testing, and I'm using the VS 2010 unit testing framework.</p> <p>I've got a function that grabs an integer from the user, then executes different functions based on user input. I've read a lot on unit testing but I haven't found anything that shows me how to test each branch of a switch statement. What I've got so far:</p> <pre><code> [TestMethod] public void RunBankApplication_Case1() { using (var sw = new StringWriter()) { using (var sr = new StringReader("1")) { Console.SetOut(sw); Console.SetIn(sr); BankManager newB = new BankManager(); newB.RunBankApplication(); var result = sw.ToString(); string expected = "Enter Account Number: "; Assert.IsTrue(result.Contains(expected)); } } } </code></pre> <p>When the function under case 1 gets called, the first thing that happens is the string "Enter Account Number: " gets written to the console. However, this isn't working at all. Am I not passing input to the console correctly? Thanks for the help!</p> <p>Edit: my RunBankApplication() function:</p> <pre><code>do { DisplayMenu(); option = GetMenuOption(); switch (option) { case 1: if (!CreateAccount()) { Console.WriteLine("WARNING: Could not create account!"); } break; case 2: if (!DeleteAccount()) { Console.WriteLine("WARNING: Could not delete account!"); } break; case 3: if (!UpdateAccount()) { Console.WriteLine("WARNING: Could not update account!"); } break; case 4: DisplayAccount(); break; case 5: status = false; break; default: Console.WriteLine("ERROR: Invalid choice!"); break; } } while (status); </code></pre>
 

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