Asp.net mvc 具有依赖注入和MOQ的单元测试
我只是在学习依赖注入和模拟是如何工作的,但我希望得到一些关于我如何设置几个测试的反馈。我可以让他们通过,但我不确定这就是我所需要的 这是一个MVC应用程序,它通过调用Web API来返回数据。对于本例,我在填充下拉列表的Web API中运行查询 请给我任何和所有的建议,关于我在这里做的是对的还是错的,或者任何我应该做的不同的事情 依赖项注入的设置文件-Unity.WebAPI(NuGet软件包) UnityConfig.csAsp.net mvc 具有依赖注入和MOQ的单元测试,asp.net-mvc,unit-testing,mocking,unity-container,moq,Asp.net Mvc,Unit Testing,Mocking,Unity Container,Moq,我只是在学习依赖注入和模拟是如何工作的,但我希望得到一些关于我如何设置几个测试的反馈。我可以让他们通过,但我不确定这就是我所需要的 这是一个MVC应用程序,它通过调用Web API来返回数据。对于本例,我在填充下拉列表的Web API中运行查询 请给我任何和所有的建议,关于我在这里做的是对的还是错的,或者任何我应该做的不同的事情 依赖项注入的设置文件-Unity.WebAPI(NuGet软件包) UnityConfig.cs public static class UnityConfig {
public static class UnityConfig
{
public static void RegisterComponents()
{
var container = new UnityContainer();
// register all your components with the container here
// it is NOT necessary to register your controllers
// e.g. container.RegisterType<ITestService, TestService>();
container.RegisterType<IDropDownDataRepository, DropDownDataRepository>();
GlobalConfiguration.Configuration.DependencyResolver = new UnityDependencyResolver(container);
}
}
存储库
public class DropDownDataRepository : IDropDownDataRepository
{
//Is this fine in here, or should it be injected somehow too?
private MyDatabaseEntities db = new MyDatabaseEntities();
public HttpResponseMessage DateList()
{
var sourceQuery = (from p in db.MyProcedure()
select p).ToList();
string result = JsonConvert.SerializeObject(sourceQuery);
var response = new HttpResponseMessage();
response.Content = new StringContent(result, System.Text.Encoding.Unicode, "application/json");
return response;
}
}
接口
public interface IDropDownDataRepository
{
HttpResponseMessage DateList();
}
单元测试
/// <summary>
/// Tests the DateList method is run
/// I pieced this kind of test together from examples online
/// I'm assuming this is good for a simple test
/// </summary>
[TestMethod]
public void DateListTest1()
{
//Arrange
var mockRepository = new Mock<IDropDownDataRepository>();
mockRepository.Setup(x => x.DateList());
var controller = new DropDownDataController(mockRepository.Object);
//Act
controller.DateList();
//Assert
mockRepository.VerifyAll();
}
/// <summary>
/// Tests the DateList method returns correct status code.
/// This will run with success, but I'm not sure if that's just
/// because I'm telling it to return what I'm expecting.
/// I welcome suggestions for improvement.
/// </summary>
[TestMethod]
public void DateListTest2()
{
//Arrange
var mockRepository = new Mock<IDropDownDataRepository>();
mockRepository
.Setup(x => x.DateList())
//This will only succeed if I have the Returns property here,
//but isn't that just bypassing the actual "test" of whether or
//not this works?
.Returns(new HttpResponseMessage(HttpStatusCode.OK));
DropDownDataController controller = new DropDownDataController(mockRepository.Object);
controller.Request = new HttpRequestMessage();
controller.Configuration = new HttpConfiguration();
//Act
var response = controller.DateList();
//Assert
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
}
//
///测试DateList方法是否运行
///我从网上的例子中拼凑出这种测试
///我假设这对于一个简单的测试是好的
///
[测试方法]
公共无效DateListTest1()
{
//安排
var mockRepository=new Mock();
mockRepository.Setup(x=>x.DateList());
var控制器=新的DropDownDataController(mockRepository.Object);
//表演
controller.DateList();
//断言
mockRepository.VerifyAll();
}
///
///测试DateList方法是否返回正确的状态代码。
///这将成功运行,但我不确定这是否只是
///因为我告诉它要回报我所期待的。
///我欢迎提出改进建议。
///
[测试方法]
公共无效DateListTest2()
{
//安排
var mockRepository=new Mock();
模拟存储库
.Setup(x=>x.DateList())
//只有当我在这里有Returns属性时,这才会成功,
//但这难道不只是绕过了真正的“测试”吗
//这不管用吗?
.Returns(新的HttpResponseMessage(HttpStatusCode.OK));
DropDownDataController控制器=新的DropDownDataController(mockRepository.Object);
controller.Request=new-HttpRequestMessage();
controller.Configuration=新的HttpConfiguration();
//表演
var response=controller.DateList();
//断言
AreEqual(HttpStatusCode.OK,response.StatusCode);
}
更新1
我这里的一个主要问题是.Returns
属性实际上做了什么。在我的第二个单元测试中,我告诉它返回OK,然后检查它是否返回OK。我看不出这到底是在测试什么
我这里的一个主要问题是.Returns属性实际上是什么
做在我的第二个单元测试中,我告诉它返回OK,然后检查
如果它返回OK。我看不出这到底是在测试什么
守则:
mockRepository
.Setup(x => x.DateList())
//This will only succeed if I have the Returns property here,
//but isn't that just bypassing the actual "test" of whether or
//not this works?
.Returns(new HttpResponseMessage(HttpStatusCode.OK));
表示当mockRepository
接收到对DateList()
的调用时,它应该返回新的HttpResponseMessage(HttpStatusCode.OK)
所以在里面
[HttpGet]
public HttpResponseMessage DateList()
当单元测试到达测试线时
return _dropDownDataRepository.DateList();
模拟对象激发并返回新的HttpResponseMessage(HttpStatusCode.OK)
此测试的更好名称应该是DateListTest2
类似于DateList\u从存储库返回\u Status\u code\u的内容,因为这是您在测试中安排的内容
老实说,controller.DateList()
没有太多的逻辑,所以这大概是你唯一可以进行的黄金路径测试。也许把这个问题发布在上面会更好?谢谢。我从来不知道它的存在,但我也会在那里尝试。谢谢你的回答。我有另一个基于同样的设置,但有一点更复杂,涉及Windows身份验证。如果你能看一看,我将不胜感激。
return _dropDownDataRepository.DateList();