6

我想让我的 ServiceStack 服务可测试。

目前我有:

[RequireFormsAuthentication]
public object Delete(DeleteRequest request)
{
     var originalRequest = (HttpRequest)Request.OriginalRequest;
     var identity = originalRequest.RequestContext.HttpContext.User.Identity;
     return othercode(identity);
}

RequireFormsAuthentication 在哪里

public class RequireFormsAuthenticationAttribute : RequestFilterAttribute
{
    public override void Execute(IHttpRequest req, IHttpResponse res, object requestDto)
    {
        var originalRequest = (HttpRequest)req.OriginalRequest;
        var identity = originalRequest.RequestContext.HttpContext.User.Identity;
        if (!identity.IsAuthenticated)
        {
            res.StatusCode = (int)HttpStatusCode.Forbidden;
            res.EndServiceStackRequest(skipHeaders: true);
        }
    }
}

我已经模拟了“othercode()”使用的所有依赖项,剩下的就是基类 Service 中的东西。是否有一种模式/策略/方法/我缺少的东西让这变得微不足道?

4

3 回答 3

7

以下是使用 Moq 进行测试的方法。此服务在查询字符串中查找“键”和“值”,并在请求 DTO 中查找另一个参数。该服务根据给定的值返回一个字符串响应。

    [Test]
    public void MyTest()
    {
        var mockRequestContext = new Mock<IRequestContext>();
        var mockedHttpRequest = new Mock<IHttpRequest>();

        NameValueCollection querystring = new NameValueCollection();
        querystring.Add("myKey", "myValue");

        mockedHttpRequest.SetupGet(r => r.QueryString).Returns(querystring);

        mockRequestContext.Setup(x => x.Get<IHttpRequest>()).Returns(mockedHttpRequest.Object);

        AboutService service = new AboutService
        {
            RequestContext  = mockRequestContext.Object,
        };

        AboutResponse response = (AboutResponse)service.Any(new About
        {
            Company = "myOtherValue",
        });

        Assert.AreEqual(0, response.QueryResult);
        Assert.AreEqual("validResponse", response.Version);
    }
于 2013-07-01T13:14:11.450 回答
2

我很抱歉没有使用最小起订量……已经使用 RhinoMocks 完成了其中的一些工作。我认为这个概念应该转移到起订量。 可能是一个很好的资源以及this

无论如何,我认为下面的测试代码应该可以帮助您入门。您的模拟接缝Request.OriginalRequest正在Service.RequestContext用模拟对象替换。然后你只需要嘲笑除此之外的一切。这将是很多“嘲笑”,如果你每次嘲笑一堂课时都对自己重复“你在嘲笑我吗”,那几乎是令人愉快的。

[Test]
public void testsomethign()
{
    var mockedRequestContext = MockRepository.GenerateMock<IRequestContext>();
    var mockedHttpRequest = MockRepository.GenerateMock<IHttpRequest>();
    var mockedOriginalRequest = MockRepository.GenerateMock<HttpRequestBase>();
    var mockedOriginalRequestContext = MockRepository.GenerateMock<RequestContext>();

    mockedOriginalRequest.Stub(x => x.RequestContext).Return(mockedOriginalRequestContext);
    mockedHttpRequest.Stub(x => x.OriginalRequest).Return(mockedOriginalRequest);

    mockedRequestContext.Stub(x => x.Get<IHttpRequest>()).Return(mockedHttpRequest);
    var service = new ServiceTests()
    {
        RequestContext = mockedRequestContext
    };

    service.Delete(new DeleteRequest());
}
于 2013-04-25T22:41:47.890 回答
2

Be sure to check out the namespace: ServiceStack.ServiceInterface.Testing.
In there you can find a MockRequestContext that you can use as follows:

var mockContext = new ServiceStack.ServiceInterface.Testing.MockRequestContext();
//do stuff to set it up if desired...

AboutService service = new AboutService
{
    RequestContext  = mockContext
};
于 2016-08-23T18:18:26.630 回答