我使用 Nunit 框架为 ServiceStack api 编写单元测试。代码如下
public class AppHost : AppHostBase
{
public AppHost()
: base("SearchService", typeof(SearchService).Assembly)
{
}
}
服务等级如下
public class SearchService:Service
{
public SearchResponse Get(SearchRequest request)
{
// to stuff
Response.StatusCode = (int)HttpStatusCode.OK;
return SearchReponse;
}
}
单元测试类如下
[TestFixture]
public class SearchServiceTests
{
private readonly ServiceStackHost appHost;
public SearchServiceTests()
{
appHost = new BasicAppHost(typeof(SearchService).Assembly)
{
}.Init();
}
[TestFixtureTearDown]
public void TestFixtureTearDown()
{
appHost.Dispose();
}
[Test]
public void TestMethod1()
{
var service = appHost.Container.Resolve<SearchService>();
var r= service.Get(new SearchRequest());
Assert.That(r, Is.Not.Null);
}
}
我得到 Response 对象的空引用异常。当我使用任何客户端(邮递员或休息客户端)访问服务器时,响应对象正在初始化但通过单元测试响应对象没有被初始化,谁能告诉我为什么会这样?提前致谢。