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

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

问题描述

我目前正在评估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服务就像任何正常的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.

否则,您可以像任何其他类一样进行单元测试和Mock操作,例如:

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天全站免登陆