chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
+3 votes

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()
            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
            var getWidgetAsyncResponse = controllerUnderTest.GetWidgetAsync(appId);
            Isolate.Verify.NonPublic.WasCalled(controllerUnderTest, "RetrieveWidget");
My understanding is when I do this:
    var controllerUnderTest = Isolate.Fake.Dependencies<WidgetApplication>();
it should create all dependencies of the <Widget>
asked by wayne (1.2k points)

Please log in or register to answer this question.