Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 mocking object.

But what this It.IsAny<>() means here?

 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");
like image 552
Sweetie Avatar asked Sep 09 '16 14:09

Sweetie


People also ask

What does it IsAny mean?

4. It. IsAny<Guid>() means that you don't care what parameter was passed.

When to use Moq?

You can use Moq to create mock objects that simulate or mimic a real object. Moq can be used to mock both classes and interfaces. However, there are a few limitations you should be aware of. The classes to be mocked can't be static or sealed, and the method being mocked should be marked as virtual.

What is mock in unit testing c#?

Mocking is a process used in unit testing when the unit being tested has external dependencies. The purpose of mocking is to isolate and focus on the code being tested and not on the behavior or state of external dependencies.

When to use mock c#?

Mock is used to test object that cannot function in isolation. suppose function A is dependent on function B, to perform unit testing on function A we even end up testing function B.By using mock you can simulate the functionality of function B and testing can be focussed only on function A.


2 Answers

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.

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.

It.Is<T> allows you to inspect what was passed in and determine if the parameter that was passed in meets your needs.

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.

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);. You don't need to use It.Is to check for reference equality, you can just pass the object directly.

like image 78
ChrisO Avatar answered Oct 05 '22 06:10

ChrisO


It.IsAny<T>() specifies anything thats of that type.

It.Is<T>() is more specific and takes a lamda to make sure it matches that exactly.

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.

If you always want the name to begin with "S" then you can do

It.Is<string>(x => x.StartsWith("S")) which will only match strings starting with S.

like image 41
gmn Avatar answered Oct 05 '22 06:10

gmn