I wrote a method in a view that cause me no end of grief when testing its presenter the other day.

I’ll elaborate on this a little more when I get the time but for now, here are the bones of the issue:

  1. public bool TrySetSummaryContent(string content, out Exception raisedError)
  2.         {
  3.             raisedError = null;
  4.             try
  5.             {
  6.                 webBrowser.DocumentText = content;
  7.             }
  8.             catch (Exception ex)
  9.             {
  10.                 raisedError = ex;
  11.                 return false;
  12.             }
  13.             return true;
  14.         }

This method is called by the presenter method below:

  1. bool ContentSet(string _summaryContent)
  2. {
  3.     // This loop is used to try setting the content of the view 3 times before erroring out.
  4.     int SetContentAttempt = 0;
  5.     Exception exRaised;
  6.     do
  7.     {
  8.         if (View.TrySetSummaryContent(_summaryContent, out exRaised))
  9.         {
  10.             return true;
  11.         }
  12.         SetContentAttempt++;
  13.     } while (SetContentAttempt<> LogError(exRaised, "The retention reason summary could not be displayed", "Summary Unavailable","Web Browser control raised an exception.");
  14.     return false;
  15. }

To write a unit test for the Content Set Method, I mocked out the view object and had to stipulate not only that the method would return a true or false value but I also had to state that the raisedError exception object in the TrySetSummaryContent method will be instantiated as so:

  1. string op = "Test Content";
  2.             Expect.Once
  3.             .On(_mockView)
  4.             .Method("TrySetSummaryContent")
  5.             .With(Is.EqualTo(op), Is.Out)
  6.             .Will(new SetNamedParameterAction("raisedError", new Exception()), Return.Value(true));

Stuck every time you jump out of the tutorials?

Make 2019 the year you finally conquer all those controllers, views, models and all that other "stuff" that comes with MVC.

You'll also get ASP.NET advice, guidance and useful tips from me once a week (or so). I respect your email privacy, unsubscribe at any time.

Stuck every time you jump out of the tutorials?

Stop wasting time trying to learn ASP.NET Core MVC and fast forward to the fun bit (building your features)

Can't make sense of front-end development?

Check out Front-end in four hours for a head-start

Take a look

Stuck every time you jump out of the tutorials?

Make 2019 the year you finally conquer all those controllers, views, models and all that other "stuff" that comes with MVC.

You'll also get ASP.NET advice, guidance and useful tips from me once a week (or so). I respect your email privacy, unsubscribe at any time.