-4

Hi Please let me know how to write the moq unit test case for the below method.Any help would be appreciated .Thanks in advance.

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

Looking for the whole method not the stub.Cheers!!!!!!!!!!!!

Nkosi
  • 235,767
  • 35
  • 427
  • 472

1 Answers1

0

Assuming a class like

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

You will only need Moq for the interface. A test could look like this.

[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
    }
}
Nkosi
  • 235,767
  • 35
  • 427
  • 472