Service stack and Mocking, any tutorials?

后端 未结 1 1865
感动是毒
感动是毒 2020-12-14 22:44

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 s

相关标签:
1条回答
  • 2020-12-14 23:04

    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.

    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"));
    
    0 讨论(0)
提交回复
热议问题