如何测试ServiceStack服务使用起订量 [英] How to test ServiceStack Service using Moq

查看:180
本文介绍了如何测试ServiceStack服务使用起订量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我与ServiceStack创建,使用NHibernate作为从SQLCE数据库中获取数据的方式休息服务。我一直在试图写使用NUnit和起订量一些单元测试 - 我已经成功地嘲笑NHibernate的执行返回null,无效对象等等 - 但我的测试中总是抛出一个的NullReferenceException 时,它调用基类设置的HTTPStatus等

 公开名单< ParameterDto>获取(ParameterAll parameterAll)
{
名单,LT; ParameterDto> parameterResponseList =新的List< ParameterDto> ();

{
名单,LT; ParameterDomainObject> parameterDomainObjectList = _ParameterDao.getAllParameters();
的foreach(ParameterDomainObject parameterDomainObject在parameterDomainObjectList)
{
parameterResponseList.Add(parameterDomainObject.ToDto());
}
}
赶上(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);
}
赶上(DomainObjectNotFoundException domainObjectNotFoundException)
{
base.Response.StatusCode =(INT)HttpStatusCode.NotFound;
base.Response.AddHeader(理,
domainObjectNotFoundException.Message);
}
赶上(例外的例外)
{
的Debug.WriteLine(异常:+ exception.Message);
base.Response.StatusCode =(INT)HttpStatusCode.InternalServerError;
base.Response.AddHeader(理,
exception.Message);
}
/ *总是在这里抛出一个异常,或任何base.Response被称为* /
base.Response.StatusCode =(INT)HttpStatusCode.OK;
base.Response.AddHeader(理,
Strings.ParameterRestResponse_Get_OK);
返回parameterResponseList;
}



与RESTClient实现和Firefox测试时,该服务工作正常,当我发表评论出了 base.Response 代码,所以我猜我只是没有设置也许东西了正确的单元测试?

  [测试] 
公共无效Test_Method_Get_AllParameters_Unsucessful()
{
&模拟LT; IRequestContext> mockedRequestContext =新的模拟< IRequestContext>();
mockedRequestContext.SetupGet(F => f.AbsoluteUri).Returns(HTTP:/本地主机:8080 /参数/全部);

&模拟LT; IParameterDao> mockedParameterDao =新的模拟< IParameterDao>();
mockedParameterDao.Setup(收益=> returns.getAllParameters())返回(新名单< ParameterDomainObject>());
Assert.IsNotNull(mockedParameterDao);

ParameterRestService服务=新ParameterRestService(mockedParameterDao.Object)
{
的RequestContext = mockedRequestContext.Object
};

名单,LT; ParameterDto> parameterDtos = service.Get(新ParameterAll());
}


解决方案

它看起来像你只需要嘲笑服务类的响应性能。它的保护,没有二传手,但你应该能够模拟它做这样...



的东西

 模拟< IRequestContext> mockedRequestContext =新的模拟< IRequestContext>(); 
&模拟LT; IHttpResponse> mockedResponse =新的模拟< IHttpResponse>();
mockedRequestContext.SetupGet(F => f.AbsoluteUri).Returns(HTTP:/本地主机:8080 /参数/全部);
mockedRequestContext.Setup(F => f.Get< IHttpResponse>())返回(mockedResponse.Object)。


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 ());
    }

解决方案

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);

这篇关于如何测试ServiceStack服务使用起订量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆