单元测试的HTTPRequest头与ServiceStack [英] Unit Test HTTPRequest Headers with ServiceStack
问题描述
我有这样的服务:
public class PlayerService : Service
{
public IPlayerAppService PlayerAppService { get; set; }
public PlayerService (IPlayerAppService service)
{
if (service == null)
throw new ArgumentException ("Service null");
PlayerAppService = service;
}
public object Post (PlayerDTO request)
{
var newPlayer = new PlayerResponse ()
{
Player = PlayerAppService.SendPlayerLocation(request.Position.Latitude, request.Position.Longitude)
};
return new HttpResult (newPlayer)
{
StatusCode = System.Net.HttpStatusCode.Created,
Headers =
{
{ HttpHeaders.Location, base.Request.AbsoluteUri.CombineWith(newPlayer.Player.Id.ToString()) }
}
};
}
}
我已经手动验证了定位和响应看起来从我这个服务的部署是正确的。我想弄清楚如何单元测试这个虽然。我写了一个测试是这样的:
I've manually verified that the Location and the Response looks correct from my deployments of this service. I would like to figure out how to unit test this though. I wrote a test like this:
[TestFixture]
public class PlayerServiceTests
{
AppHost appHost;
[TestFixtureSetUp]
public void TestFixtureSetUp ()
{
appHost = new AppHost ();
appHost.Init ();
appHost.Start ("http://localhost:1337/");
}
[TestFixtureTearDown]
public void TestFixtureTearDown ()
{
appHost.Dispose ();
appHost = null;
}
[Test]
public void NewPlayer_Should_Return201AndLocation ()
{
// Arrange
PlayerService service = new PlayerService (appHost.TryResolve<IPlayerAppService>());
// Act
HttpResult response = (HttpResult)service.Post (It.IsAny<PlayerDTO>());
// Assert
Assert.NotNull (response);
Assert.AreEqual(HttpStatusCode.Created, response.StatusCode);
Assert.AreEqual(response.Response.ToDto<PlayerResponse>().Player.Id.ToString(), response.Headers.Where(x=> x.Key == HttpHeaders.Location).SingleOrDefault().Value);
}
}
的
base.Request当我的单元测试,虽然运行。你有我如何能在我的单元测试填充这个有什么建议?
The base.Request when my unit test runs though. Do you have any suggestions on how I can populate this from my unit test?
推荐答案
您使用的是自托管的HttpListener,你会为一个集成测试,但你不是在做整合。测试
You're using an self-hosting HttpListener as you would for an integration test, but you're not doing in integration test.
这是集成测试看起来像:
An integration test would look like:
[Test]
public void NewPlayer_Should_Return201AndLocation ()
{
var client = new JsonServiceClient("http://localhost:1337/");
client.LocalHttpWebResponseFilter = httpRes => {
//Test response headers...
};
PlayerResponse response = client.Post(new Player { ... });
}
否则,如果你想要做一个单元测试,你不需要APPHOST并且可以只测试 PlayerService
类,就像任何其他C#类,注射所有的依赖,它需要模拟请求上下文。
Otherwise if you want to do an unit test you don't need an AppHost and can just test the PlayerService
class just like any other C# class, injecting all the dependencies and the mock Request context it needs.
[Test]
public void NewPlayer_Should_Return201AndLocation ()
{
var mockCtx = new Mock<IRequestContext>();
mockCtx.SetupGet (f => f.AbsoluteUri).Returns("localhost:1337/player");
PlayerService service = new PlayerService {
MyOtherDependencies = new Mock<IMyOtherDeps>().Object,
RequestContext = mockCtx.Object,
};
HttpResult response = (HttpResult)service.Post(new Player { ... });
//Assert stuff..
}
这篇关于单元测试的HTTPRequest头与ServiceStack的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!