如何使用moq对请求对象进行单元测试

时间:2014-08-27 18:37:34

标签: c# moq

我遇到了一个问题,我正在对我的请求对象进行单元测试。我的方法是在服务中,在调用存储库方法之前,在服务方法中调用我的数据访问代码所在的存储库。我正在调用isValid方法,如果请求有效,则返回true。在请求对象中我有UpdatedBy我想测试它。

你们能告诉我怎么做吗?我的代码是:

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

我正在使用Moq,并且expection是UpdatedBy_ShouldSet

1 个答案:

答案 0 :(得分: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");
}
相关问题