我是ServiceStack的新手,正在尝试理解其不同部分的工作原理和配合方式。这是我正在使用的简单服务:
[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 };
}
}
...这是单元测试:
[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");
}
}
在上面的单元测试类中,我将服务类用作普通的C#类... 当然,单元测试是成功的。但如果我像这样重新编写单元测试会怎样(基于我在互联网上找到的一个例子)?
[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");
}
}
结果完全相同,那么我为什么要使用模拟工具呢?