| name | servicestack-testing |
| description | Tests services using ServiceStack’s testing utilities and in-memory hosts. |
ServiceStack Testing
ServiceStack provides excellent support for both unit and integration testing.
Unit Testing
- Test services in isolation.
- Manually inject dependencies into the
Service class.
var service = new MyService { Db = dbFactory.Open() };
var response = service.Any(new MyRequest { ... });
Assert.That(response.Result, Is.EqualTo("Expected"));
Integration Testing
- Use
BasicAppHost or InMemoryAppHost for light integration tests.
- Use a full
AppHost (self-hosted or TestServer) for end-to-end tests with typed clients.
public class MyIntegrationTest : ServiceStackTestBase
{
public MyIntegrationTest()
{
ConfigureAppHost(new AppHost());
}
[Test]
public void Can_call_service()
{
var client = CreateClient();
var response = client.Get(new MyRequest { ... });
}
}
Mocking
ServiceStack is designed for easy DI, so you can easily swap real implementations with mocks (e.g., Moq, NSubstitute) in your AppHost configuration during tests.
Best Practices
- SQLite In-Memory: Use SQLite in-memory for fast database testing.
- Isolation: Ensure each test is independent and resets any global state.
- Contract Testing: Focus on verifying the DTO contracts to ensure clients won't break.
- AppHost Reuse: Share a common
AppHost configuration for related integration tests.