vendredi 9 septembre 2016

What is it.isAny and what is it.is in Unit mock testing

There are many questions that have been already asked on this but I think I need something more basic that could clear this concept as I am beginner in TDD. I can't go forward till then.

Could you please go through following testmethod and explain if I have wrong understanding:

[Test]
public void ShouldSearch()
{
         var ColumnList = new List<Column>();

         The below line means that I am going to create a fake object to not actually call GetColumn method because it has dependency.
         As the GetColumn is asking for two parameters, I have passed it as below. One is type of context and other is of Column.
         But what this "It.IsAny<>() means here? 

        Some say that this statement will return me the ColumnList if the Getcolumn method is called by context parameter
         and Column parameter. How can this be true? 

        Because I am no way calling this "GetColumn" method actually. It is just a mock or just a signature which is saying that one parameter should be of type context and other should be of column type because this
         method has signature likewise.  
 this.ColumnServiceMock.Setup(x => x.GetColumn(It.IsAny<Context>(), It.IsAny<Column>())).Returns(ColumnList);

        var result = this.getColouminfo.GetFinalRecords(this.context, this.gridColumn);


        // Is it like I am calling Getcolumn here? IF yes, Then what this It.Is means here?
        this.ColumnServiceMock.Verify(x => x.GetColumn(It.Is<Context>(y => y == this.context),
            It.Is<Column>(y => y.Id == 2)), Times.Once);

        Assert.AreEqual(1, result.Data.Count, "Not equal");

        Assert.IsTrue(result.Data.Success, "No success");

Aucun commentaire:

Enregistrer un commentaire