Reputation: 9724
I'm very new to ServiceStack and I'm trying to understand how the different parts work and fit together. Here is the simple service I'm playing with:
[Route("/welcome")]
[Route("/welcome/{Name}")]
public class Welcome
{
public string Name { get; set; }
}
public class WelcomeResponse
{
public string Result { get; set; }
}
public class WelcomeService : Service
{
public object Any(Welcome request)
{
return new WelcomeResponse { Result = "Welcome " + request.Name };
}
}
... and here is the unit test:
[TestFixture]
public class WelcomeServiceTest
{
[Test]
public void Welcome()
{
var service = new WelcomeService();
var response = (WelcomeResponse) service.Any(new Welcome { Name = "Gonzo" });
Assert.That(response.Result == "Welcome Gonzo");
}
}
In the unit test class above I'm using the service classes as normal C# classes... and of course the unit test succeeds. But what if I rewrite the unit test like this (based on an example I've found on the Internet)?
[TestFixture]
public class WelcomeServiceTest
{
[Test]
public void Welcome()
{
var context = new Mock<IRequestContext>();
context.SetupGet(f => f.AbsoluteUri).Returns("localhost:8888/welcome");
var service = new WelcomeService {
RequestContext = context.Object
};
var response = (WelcomeResponse) service.Any(new Welcome { Name = "Gonzo" });
Assert.That(response.Result == "Welcome Gonzo");
}
}
The result is exactly the same... so why should I use the mock stuff?
Upvotes: 3
Views: 1608
Reputation: 143284
See this previous answer for different Unit Testing options in ServiceStack.
Here are also some examples of Integration Testing. What you choose is a preference of what you want and how much you want to test, e.g. An integration test effectively tests the live service as hosted inside ServiceStack instance, complete with any custom filters or plugins. A unit test only tests your Services implementation, which is easier and faster.
Upvotes: 2