【问题标题】:How to test ServiceStack Service using Moq如何使用 Moq 测试 ServiceStack 服务
【发布时间】:2013-07-13 02:56:35
【问题描述】:

我有一个使用 ServiceStack 创建的休息服务,使用 nHibernate 作为从 SqlCe 数据库获取数据的一种方式。我一直在尝试使用 nUnit 和 Moq 编写一些单元测试——我已经成功地模拟了 nHibernate 实现以返回 null、无效对象等——但是我的测试在调用基类来设置时总是抛出 NullReferenceException HttpStatus 等

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

该服务在使用 RestClient 和 Firefox 进行测试时运行良好,当我注释掉 base.Response 代码时,我猜我可能只是没有在单元测试中设置正确的东西?

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

【问题讨论】:

  • 我还不熟悉 ServiceStack ……但是基类是什么?
  • 据我所知(仍在自己学习!)它是 Apphost。我正在尝试在没有 AppHost 的情况下运行服务,因为如果我使用我无法设置 Mock 对象AppHost,反正我不知道 :)
  • 并不是说这是 [stackoverflow.com/questions/9671493/…] 重复,但看看它是否有帮助。它正在处理针对 AppHost 的 nUnit 和单元测试。
  • 但是,这个 [stackoverflow.com/questions/14790695/…] 可能会回答您的问题。
  • 啊,对了,我用它作为起点,但同样的问题发生了,不知道发生了什么,但仍然觉得它很简单,只需要设置!

标签: c# unit-testing moq servicestack


【解决方案1】:

看起来您只需要模拟 Service 类的 Response 属性。它没有设置器受到保护,但你应该能够模拟它做类似的事情......

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

【讨论】:

    猜你喜欢
    • 2021-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-24
    相关资源
    最近更新 更多