简体   繁体   English

单元模拟测试中的 it.isAny 和 it.is 是什么

[英]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.已经有很多关于此的问题,但我认为我需要一些更基本的东西来清除这个概念,因为我是 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 mocking object.下面这行表示我在嘲笑对象。

But what this It.IsAny<>() means here?但是这里It.IsAny<>()是什么意思?

 this.ColumnServiceMock.Setup(x => x.GetColumn(It.IsAny<Context>(), It.IsAny<Column>()))
                       .Returns(ColumnList);

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

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

It.IsAny<T> is checking that the parameter is of type T, it can be any instance of type T. It's basically saying, I don't care what you pass in here as long as it is type of T. It.IsAny<T>正在检查参数是否为 T 类型,它可以是 T 类型的任何实例。基本上是说,我不在乎你在这里传入什么,只要它是 T 类型。

this.ColumnServiceMock.Setup(x => x.GetColumn(It.IsAny<Context>(), It.IsAny<Column>())).Returns(ColumnList);

The above is saying whenever the GetColumn method is called with any parameters (as long as they are type of Context and Column respectively), return the ColumnList .上面是说,无论何时使用任何参数调用GetColumn方法(只要它们分别是ContextColumn类型),都返回ColumnList

It.Is<T> allows you to inspect what was passed in and determine if the parameter that was passed in meets your needs. It.Is<T>允许您检查传入的内容并确定传入的参数是否满足您的需求。

this.ColumnServiceMock.Verify(x => x.GetColumn(It.Is<Context>(y => y == this.context), It.Is<Column>(y => y.Id == 2)), Times.Once);

The above is asserting that the GetColumn method was called exactly once with the Context parameter equal to this.Context and a Column parameter whose Id property equals 2.上面断言GetColumn方法被调用了一次, Context参数等于this.ContextColumn参数的 Id 属性等于 2。

Edit: Revisiting this answer years later with some more knowledge.编辑:几年后用更多的知识重新审视这个答案。 this.ColumnServiceMock.Verify(x => x.GetColumn(It.Is<Context>(y => y == this.context), It.Is<Column>(y => y.Id == 2)), Times.Once); can be shortened to this.ColumnServiceMock.Verify(x => x.GetColumn(this.context, It.Is<Column>(y => y.Id == 2)), Times.Once);可以缩短为this.ColumnServiceMock.Verify(x => x.GetColumn(this.context, It.Is<Column>(y => y.Id == 2)), Times.Once); . . You don't need to use It.Is to check for reference equality, you can just pass the object directly.您不需要使用 It.Is 来检查引用相等性,您可以直接传递对象。

It.IsAny<T>() specifies anything thats of that type. It.IsAny<T>()指定该类型的任何内容。

It.Is<T>() is more specific and takes a lamda to make sure it matches that exactly. It.Is<T>()更具体,需要一个 lamda 来确保它完全匹配。

Both are just ways to specify an argument that you don't want to specify exactly when mocking.两者都只是指定参数的方法,您不想在模拟时准确指定。 So for example if the argument is a string name and you don't care about the exact name in your test you can use:因此,例如,如果参数是字符串名称并且您不关心测试中的确切名称,则可以使用:

It.IsAny<string>() in your mock specification, which will match any string. It.IsAny<string>()在您的模拟规范中,它将匹配任何字符串。

If you always want the name to begin with "S" then you can do如果您总是希望名称以“S”开头,那么您可以这样做

It.Is<string>(x => x.StartsWith("S")) which will only match strings starting with S. It.Is<string>(x => x.StartsWith("S"))只会匹配以 S 开头的字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM