I'm trying to unit test this code:
public async Task<Messaging.BaseTypes.BaseWidget> GetWidgetAsync(string widgetId)
{
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
//to return null
//Act
var getWidgetAsyncResponse = controllerUnderTest.GetWidgetAsync(appId);
//Assert
Isolate.Verify.NonPublic.WasCalled(controllerUnderTest, "RetrieveWidget");
Assert.IsNull(getWidgetAsyncResponse.Result);
}
My understanding is when I do this:
var controllerUnderTest = Isolate.Fake.Dependencies<WidgetApplication>();
it should create all dependencies of the <Widget>