Moq使用out参数测试void方法

时间:2016-09-02 05:02:42

标签: c# unit-testing moq

嗨请告诉我如何编写以下方法的moq单元测试用例。非常感谢。谢谢。提前谢谢。

public void ConstructAddMappingQuery(IAnnotationMapping annotationMappings, 
                                     out string commandText, 
                                     out Dictionary<string, dynamic> parameters)
{
   commandText =@"Insert Into AnnotationMapping Values 
                 (@AnnotationSetupId, @WordToAnnotate, 
                  @Annotation, @CreatedDttm, @CreatedUserId, @ModifiedDate, 
                  @ModifiedUserId, @IsActive)";

   parameters = new Dictionary<string, dynamic>();
   parameters.Add("@WordToAnnotate", annotationMappings.WordToAnnotate);
   parameters.Add("@Annotation", annotationMappings.Annotation);
   parameters.Add("@ModifiedDate", annotationMappings.ModifiedDate);
   parameters.Add("@ModifiedUserId", annotationMappings.ModifiedUserId);
   parameters.Add("@AnnotationSetupId", annotationMappings.AnnotationSetupId);
   parameters.Add("@CreatedDttm", annotationMappings.CreatedDttm);
   parameters.Add("@CreatedUserId", annotationMappings.CreatedUserId);
   parameters.Add("@IsActive", 1);
}

寻找整个方法而不是stub.Cheers !!!!!!!!!!!!

1 个答案:

答案 0 :(得分:0)

假设有一个类

public class AnnotationMappingQueryBuilder {
    public void ConstructAddMappingQuery(IAnnotationMapping annotationMappings,
                                 out string commandText,
                                 out Dictionary<string, dynamic> parameters) {
        commandText = @"Insert Into AnnotationMapping Values 
             (@AnnotationSetupId, @WordToAnnotate, 
              @Annotation, @CreatedDttm, @CreatedUserId, @ModifiedDate, 
              @ModifiedUserId, @IsActive)";

        parameters = new Dictionary<string, dynamic>();
        parameters.Add("@WordToAnnotate", annotationMappings.WordToAnnotate);
        parameters.Add("@Annotation", annotationMappings.Annotation);
        parameters.Add("@ModifiedDate", annotationMappings.ModifiedDate);
        parameters.Add("@ModifiedUserId", annotationMappings.ModifiedUserId);
        parameters.Add("@AnnotationSetupId", annotationMappings.AnnotationSetupId);
        parameters.Add("@CreatedDttm", annotationMappings.CreatedDttm);
        parameters.Add("@CreatedUserId", annotationMappings.CreatedUserId);
        parameters.Add("@IsActive", 1);
    }
}

接口只需要Moq。测试可能看起来像这样。

[TestClass]
public class UnitTest1 {
    [TestMethod]
    public void TestMethod1() {

        //Arrange
        var mock = Mock.Of<IAnnotationMapping>();

        var expectedCommandText = @"Insert Into AnnotationMapping Values 
             (@AnnotationSetupId, @WordToAnnotate, 
              @Annotation, @CreatedDttm, @CreatedUserId, @ModifiedDate, 
              @ModifiedUserId, @IsActive)";
        string commandText = null;
        Dictionary<string, dynamic> parameters = null;
        int expectedParameterCount = 8;
        var sut = new AnnotationMappingQueryBuilder();

        //Act
        sut.ConstructAddMappingQuery(mock, out commandText, out parameters);

        //Assert
        Assert.IsNotNull(commandText);
        Assert.AreEqual(expectedCommandText, commandText);

        Assert.IsNotNull(parameters);
        Assert.AreEqual(expectedParameterCount, parameters.Count);
        ///what ever else you want to assert for parameters
    }
}