How to Test ServiceStack Service Using Moq

I have a leisure service that I created with ServiceStack, using nHibernate as a way to retrieve data from the SqlCe database. I tried to write some unit tests using nUnit and Moq - I successfully mocked the nHibernate implementation to return null, invalid objects, etc. - but my tests always throw a NullReferenceException when it calls the base class to set 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 great when testing with RestClient and Firefox, and when I comment on the base.Response code, so I guess I just can't do something right in 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 ()); } 
+4
source share
1 answer

It looks like you just need to make fun of the Response property of the Service class. It is protected without a setter, but you should be able to mock it by 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); 
+3
source

Source: https://habr.com/ru/post/943632/


All Articles