服务堆栈和模拟,任何教程? [英] Service stack and Mocking, any tutorials?

查看:25
本文介绍了服务堆栈和模拟,任何教程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在评估 ServiceStack(在 .Net 中创建基于休息的服务).感兴趣的领域之一是测试方面.我的休息服务将注入许多应用程序服务(目前使用 Autofac).我需要的是一种机制来测试其余层并在我的应用层上定义期望(通过 MOQ),所以我不是在进行集成测试而是对这一层进行单元测试?

I am currently evaluating ServiceStack (to create rest based services in .Net). One of the areas of interest is the testing side. My rest service will have a number of app services injected in (currently using Autofac). What I need is a mechanism to test the rest layer and define expectations on my app layer (via MOQ), so I am not doing integration tests but unit testing this layer?

关于如何做到这一点的任何想法?

Any ideas on how to do this?

推荐答案

A ServiceStack Service 就像任何普通的 C# 服务类一样,可以像任何其他类一样以完全相同的方式进行模拟.ServiceStack 服务的最小依赖是实现无依赖的 IService 接口标记,其中任何服务只接受请求 DTO 并返回任何对象.

A ServiceStack Service is just like any normal C# Service class and can be mocked in exactly the same way like any other class. The minimum dependency for a ServiceStack Service is implementing the dependency-free IService interface marker and where any service just accepts a Request DTO and returns any object.

单元测试 ServiceStack 服务的一种方法是使用 DirectServiceClient 作为 在本例中看到,这样做的好处是它允许您使用相同的单元测试作为集成测试 - 测试所有不同的 XML、JSON、JSV 和 SOAP 端点.

One way to Unit test ServiceStack services is to use the DirectServiceClient as seen in this example, a benefit of this is that it lets you use the same Unit Test as an integration test - testing all the different XML, JSON, JSV and SOAP endpoints.

否则你可以像任何其他类一样进行单元测试和模拟,例如:

Otherwise you can unit test and Mock it like any other class, e.g:

var service = new TestService {
   MyDependency = new Mock<IMyDependency>().Object
};
var response = service.Get(new Test { Id = 1 });
Assert.That(response.Result, Is.EqualTo("Hello, 1"));

这篇关于服务堆栈和模拟,任何教程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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