var widget = await RetrieveWidget(widgetId, true); //true here fires off other if-else logic for what to do

//with a null widget

if (widget != null) return widget.Application;

Response.StatusCode = 404; //this is the line that throws the null reference error. Says Response is not set to

//an instance of an object.

return null;

}

I can test the happy path and get it to return the widget, but when I try to test the Response.StatusCode line, the code throws a null reference error every time. What am I missing? Here is my test code.

[TestMethod, Isolated]

public void GetWidgetAsync_ShouldReturnNullAndSetResponseStatusCodeTo404_IfWidgetIsNull()

{

//Arrange

string widgetId = "12341224-1234-1234-1234-123412341234";

var controllerUnderTest = Isolate.Fake.Dependencies<Application>();

var fakeWidget = Isolate.Fake.Instance<WidgetApplication>();

Isolate.NonPublic.WhenCalled(controllerUnderTest, "RetrieveWidget").WillReturn(Task.FromResult(Task.CompletedTask)); //this is how I'm setting the RetrieveWidget call

Thanks for the link, Raphy. If I am understanding the docs correctly, in my tests I will need to instantiate any fake dependencies in that constructor if I want to test them or any methods they have? For example, I would need to add: