2016-01-05 124 views
0

我刚刚学习如何依赖注入和嘲笑的工作,但我想如何设置一些测试的一些反馈。我可以让他们通过,但我不确定这是我需要的。单元测试与依赖注入和最小起订量

这是一个使Web API调用返回数据的MVC应用程序。对于这个例子,我在填充下拉列表的Web API中运行查询。

请给我任何和所有关于我在做什么的对或错的建议,这里或任何我应该做的不同。

安装文件的依赖注入 - Unity.WebAPI(NuGet包)

UnityConfig.cs

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 DropDownDataController : ApiController 
{ 
    private IDropDownDataRepository _dropDownDataRepository; 

    //Dependency Injection (I'm using Unity.WebAPI) 
    public DropDownDataController(IDropDownDataRepository dropDownDataRepository) 
    { 
     _dropDownDataRepository = dropDownDataRepository; 
    } 

    [HttpGet] 
    public HttpResponseMessage DateList() 
    { 
     try 
     { 
      return _dropDownDataRepository.DateList(); 
     } 
     catch 
     { 
      throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotFound)); 
     } 
    } 
} 

处置库

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; 
    } 
} 

INTERFACE

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); 
} 

UPDATE 1

我的主要问题之一是.Returns属性实际上做了什么。在我的第二次单元测试中,我告诉它返回OK,然后检查它是否返回OK。我看不出如何测试任何东西。

+0

也许最好在[codereview](http://codereview.stackexchange.com)上发布这个问题? – dee

+0

谢谢。我从来不知道存在,但我也会在那里尝试。 – madvora

回答

1

我的一个主要问题就是.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()通话,那么它应该返回new HttpResponseMessage(HttpStatusCode.OK)

所以里面

[HttpGet] 
    public HttpResponseMessage DateList() 

当单元测试达到行

return _dropDownDataRepository.DateList(); 

的嘲笑对象的火灾,并返回new HttpResponseMessage(HttpStatusCode.OK)

一个更好的名字为这个测试是不是DateListTest2东西如DateList_Returns_Status_Code_From_Repository,因为这是你在测试中安排的。

说实话controller.DateList()没有太多的逻辑,所以这是唯一的黄金路径测试,你可以有。

+0

谢谢你的回答。我有另一个基于相同的设置,但涉及Windows身份验证更复杂一点。如果你能看一看,我会非常感激。 http://stackoverflow.com/questions/34519238/unit-testing-a-controller-that-uses-windows-authentication – madvora