我对 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# 类......当然单元测试成功了。但是,如果我像这样重写单元测试(基于我在 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");
}
}
结果完全一样……那我为什么要使用模拟的东西呢?