Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Service stack and Mocking, any tutorials?

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?

like image 820
JD. Avatar asked Mar 06 '12 15:03

JD.


People also ask

What are mocking tools?

Mocking is a process used in unit testing when the unit being tested has external dependencies. The purpose of mocking is to isolate and focus on the code being tested and not on the behavior or state of external dependencies.

What is mocking used for?

Mocking is a way to replace a dependency in a unit under test with a stand-in for that dependency. The stand-in allows the unit under test to be tested without invoking the real dependency.

What are mock services in testing?

What is mock testing? Mocking means creating a fake version of an external or internal service that can stand in for the real one, helping your tests run more quickly and more reliably. When your implementation interacts with an object's properties, rather than its function or behavior, a mock can be used.

What is meant by mocking data?

Mock data is fake data which is artificially inserted into a piece of software. As with most things, there are both advantages and disadvantages to doing this.


1 Answers

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"));
like image 116
mythz Avatar answered Sep 22 '22 09:09

mythz