我对单元测试非常陌生。只是想对现有的webapi控制器进行单元测试。在控制器内部,我使用 UnityContainer 解析依赖关系。
运行测试用例时,获取异常
Microsoft.Practices.Unity.ResolutionFailedException:解析 依赖失败
有人可以指导我解决这种依赖吗?在此先感谢!!
public class PQController : ApiController
{
public IHttpActionResult GetById(int pqid)
{
if (!IsValidPqIds || sourceId <= 0)
{
return BadRequest();
}
IUnityContainer container = new UnityContainer();
container.RegisterType<IPQAdapter, PQAdapterDesktop>();
var pqDesktopAdapter = container.Resolve<IPQAdapter>();
var pq = pqDesktopAdapter.GetPQByIds<PQDesktop>(pqid);
return Ok(pq);
}
}
测试代码:
[TestClass]
public class TestPriceQuoteController
{
[TestMethod]
public void Get_PriceQuoeById_Should_Return_Valid_PriceQuote()
{
var pqController = new PQController();
// Act
IHttpActionResult actionResult = pqController.GetById(55560);
var contentResult = actionResult as OkNegotiatedContentResult<PQDesktop>;
// Assert
Assert.IsNotNull(contentResult);
Assert.IsNotNull(contentResult.Content);
Assert.AreEqual(55560, contentResult.Content.PQId);
}
}