11

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?

BanksySan
  • 27,362
  • 33
  • 117
  • 216
JD.
  • 15,171
  • 21
  • 86
  • 159

1 Answers1

12

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"));
mythz
  • 141,670
  • 29
  • 246
  • 390
  • @Thanks. I think we will go servicestack for our new project. Great work. – JD. Mar 07 '12 at 08:56
  • 2
    When I mock services this way filter attributes don't seem to be triggered, is that expected behaviour? – Shagglez Jan 17 '13 at 17:56
  • When you Mock the `IService` you just using this to isolate the dependencies. All of the filter attributes depend on more of the stack running (i.e. setup/configuration/initialization specified in your AppHost class). If you need to test code in the filter attributes then the attribute classes should be tested independently. – CodeMonkeyKing Feb 12 '13 at 04:40
  • I don't understand why ServiceStack wasn't designed via TDD in the first place; we wouldn't be having this kind of discussion as there would already be 100% code coverage, a crap load of tests to check out as well as mocks. It's disappointing to our craft IMO that people are still not test driven. – PositiveGuy Sep 19 '13 at 18:48
  • 1
    @CoffeeAddict It's disappointing that people in our craft think their tech processes should be blindly imposed on every software project (and not where it actually yields a benefit) despite only a fraction of all software uses processes-driven cult-practices like TDD. We prefer Test After, especially. when designing Services APIs which we [take great care thinking about when designing services](http://stackoverflow.com/a/15941229/85785) which would not benefit for the bottom-up approach of TDD design. – mythz Sep 19 '13 at 19:05
  • retracted my statements, it's not worth a war...but I do disagree. – PositiveGuy Sep 19 '13 at 19:28
  • 1
    btw, thanks though for the link it gives me the thinking behind ServiceStack in terms of design – PositiveGuy Sep 19 '13 at 19:31
  • 1
    np :) retracted my orphaned response as well. – mythz Sep 19 '13 at 19:32