2016-01-05 204 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