4

I have a rest service that I have created with ServiceStack, using nHibernate as a way of getting the data from a SqlCe database. I've been trying to write some unit tests using nUnit and Moq - I have successfully mocked the nHibernate implementation to return null, invalid objects and so on - but my tests always throw a NullReferenceException when it calls the base class to set the HttpStatus etc.

public List <ParameterDto> Get (ParameterAll parameterAll)
    {
        List <ParameterDto> parameterResponseList = new List <ParameterDto> ();
        try
        {
            List <ParameterDomainObject> parameterDomainObjectList = _ParameterDao.getAllParameters ();
            foreach (ParameterDomainObject parameterDomainObject in parameterDomainObjectList)
            {
                parameterResponseList.Add (parameterDomainObject.ToDto ());
            }
        }
        catch (WebServiceException webEx)
        {
            Debug.WriteLine ("WebServiceException.ErrorCode         " + webEx.ErrorCode);
            Debug.WriteLine ("WebServiceException.ErrorMessage      " + webEx.ErrorMessage);
            Debug.WriteLine ("WebServiceException.ResponseStatus    " + webEx.ResponseStatus);
            Debug.WriteLine ("WebServiceException.StatusCode        " + webEx.StatusCode);
            Debug.WriteLine ("WebServiceException.StatusDescription " + webEx.StatusDescription);
            Debug.WriteLine ("WebServiceException.ErrorCode         " + webEx.ErrorCode);
        }
        catch (DomainObjectNotFoundException domainObjectNotFoundException)
        {
            base.Response.StatusCode = (int) HttpStatusCode.NotFound;
            base.Response.AddHeader ("Reason",
                                     domainObjectNotFoundException.Message);
        }
        catch (Exception exception)
        {
            Debug.WriteLine ("Exception: " + exception.Message);
            base.Response.StatusCode = (int) HttpStatusCode.InternalServerError;
            base.Response.AddHeader ("Reason",
                                     exception.Message);
        }
        /* Always throws an exception here, or anywhere base.Response is called */
        base.Response.StatusCode = (int) HttpStatusCode.OK;
        base.Response.AddHeader ("Reason",
                                 Strings.ParameterRestResponse_Get_OK);
        return parameterResponseList;
    }

The service works fine when testing it with RestClient and Firefox, and when I comment out the base.Response code so I'm guessing I'm just not setting something up right in the unit tests maybe?

    [Test]
    public void Test_Method_Get_AllParameters_Unsucessful ()
    {
        Mock <IRequestContext> mockedRequestContext = new Mock<IRequestContext>();
        mockedRequestContext.SetupGet(f => f.AbsoluteUri).Returns("http:/localhost:8080/parameters/all");

        Mock<IParameterDao> mockedParameterDao = new Mock<IParameterDao>();
        mockedParameterDao.Setup (returns => returns.getAllParameters ()).Returns (new List <ParameterDomainObject> ());
        Assert.IsNotNull (mockedParameterDao);

        ParameterRestService service = new ParameterRestService(mockedParameterDao.Object)
        {
            RequestContext = mockedRequestContext.Object
        };

        List <ParameterDto> parameterDtos = service.Get (new ParameterAll ());
    }
IAbstract
  • 19,551
  • 15
  • 98
  • 146
KingTravisG
  • 1,316
  • 4
  • 21
  • 43
  • I'm not familiar with ServiceStack (yet) ...but what is the base class? – IAbstract Apr 02 '13 at 02:06
  • As far as I know (still learning it myself!) it's the Apphost.. I'm trying to run the Service without the AppHost, since I have no way of setting the Mock objects if I use the AppHost, well not that I know of anyways :) – KingTravisG Apr 02 '13 at 02:09
  • Not that this is [ http://stackoverflow.com/questions/9671493/unit-testing-servicestack-services-with-custom-serialization-deserialization?rq=1 ] a duplicate, but take a look and see if it helps any. It is dealing with nUnit and unit testing against `AppHost`. – IAbstract Apr 02 '13 at 02:35
  • 1
    However, this [ http://stackoverflow.com/questions/14790695/unit-test-httprequest-headers-with-servicestack/14791657#14791657 ] might answer your question. – IAbstract Apr 02 '13 at 02:40
  • Ah right, I have used that as a starting point but the same problem happens, not sure what is going on but still have a feeling its something simple that just needs set! – KingTravisG Apr 02 '13 at 02:47

1 Answers1

3

It looks like you just need to mock the Response property of the Service class. It's protected with no setter but you should be able to mock it doing something like...

    Mock<IRequestContext> mockedRequestContext = new Mock<IRequestContext>();
    Mock<IHttpResponse> mockedResponse = new Mock<IHttpResponse>();
    mockedRequestContext.SetupGet(f => f.AbsoluteUri).Returns("http:/localhost:8080/parameters/all");
    mockedRequestContext.Setup(f => f.Get<IHttpResponse>()).Returns(mockedResponse.Object);
paaschpa
  • 4,816
  • 11
  • 15