2014-08-27 55 views
0

我陷入了一個單元測試我的請求對象的問題。我的方法是在服務調用存儲庫,我的數據訪問代碼是在服務方法調用存儲庫方法之前。如果請求有效,我將調用isValid方法返回true。在請求對象中我有UpdatedBy我想測試一下。如何使用moq單元測試請求對象

你們能告訴我怎麼做嗎?我的代碼是:

[TestMethod] 
public void UpdatedBy_ShouldSet() 
{ 
    OperationResponse<ItemDTO> response = new OperationResponse<ItemDTO>(); 
    AddEditRequest request = new GasketTypeRequest(); 

    request.UpdatedBy = "userid"; 

    var Items = new List<ItemDTO>() 
     { 
      new ItemDTO 
      { 
       ID = 1, 
       GID = 105, 
       NO = 24, 
       SHORTDESC = "test",      
       LONGDESC = "test", 
       STATUS = Constants.STATUS, 
       LIST = "Y", 
       Action = NameConstants.DeleteAction 
      }, 
       new ItemDTO 
      { 
       ID = 2, 
       GID = 113, 
       NO = 246, 
       SHORTDESC = "test",      
       LONGDESC = "test", 
       STATUS = Constants.STATUS, 
       LIST = "Y", 
       Action = string.Empty 
      }  
     }; 

    request.TypeList = Items; 
    _service.IsRequestValid(request, response); 
    _ItemRepository.Setup(x => x.AddEditType(request)).Returns(response); 
} 

我用的起訂量,及期望是UpdatedBy_ShouldSet

+0

@Carsten,我們似乎撞上了我們的編輯。我把你推回去,並將格式應用到代碼中。 – gunr2171 2014-08-27 18:42:01

+1

@ gunr2171嘿沒問題;) – Carsten 2014-08-27 18:42:46

+0

你測試你的服務或你的請求對象嗎?服務是負責在請求中設置UpdatedBy的人嗎?如果是這樣,那麼你只需要斷言request.UpdatedBy已設置。如果存儲庫是這樣做的,那麼你的測試應該在你的存儲庫中,而不是嘲笑它。分享你正在測試的代碼,這可能有助於澄清。 – Hammerstein 2014-08-27 18:55:16

回答

0

這裏是代碼

[TestMethod] 
public void UpdatedBy_ShouldSet() 
{ 
    OperationResponse<ItemDTO> response = new OperationResponse<ItemDTO>(); 
    AddEditRequest request = new GasketTypeRequest(); 

    request.UpdatedBy = "userid"; 

    var Items = new List<ItemDTO>() 
     { 
      new ItemDTO 
      { 
       ID = 1, 
       GID = 105, 
       NO = 24, 
       SHORTDESC = "test",      
       LONGDESC = "test", 
       STATUS = Constants.STATUS, 
       LIST = "Y", 
       Action = NameConstants.DeleteAction 
      }, 
       new ItemDTO 
      { 
       ID = 2, 
       GID = 113, 
       NO = 246, 
       SHORTDESC = "test",      
       LONGDESC = "test", 
       STATUS = Constants.STATUS, 
       LIST = "Y", 
       Action = string.Empty 
      }  
     }; 

    //Assert 
      Assert.AreEqual("userid" , request.UpdatedBy,"Updated by should not be empty"); 
}