Mock.Of<Object> VS Mock<Object>()

27,817

Solution 1

This post helped me to understand Mock.Of<T> : Old style imperative Mock<T> vs functional Mock.Of<T>

As explained in the post, with Mock.Of<T> you're saying "Give me a mock that behaves like this" (or Mocks.Of<T> if you need to get many objects (IEnumerable)). It makes the declaration of a mock more concise.

Example with Mock<T> (returns a Mock)

var el1 = new Mock<IElementInfo>();
el1.Setup(x => x.Id).Returns(Guid.NewGuid());
el1.Setup(x => x.Multiplicity).Returns(Multiplicity.Single);

var c1 = new Mock<ICollectionInfo>();
c1.Setup(x => x.Id).Returns(Guid.NewGuid());
c1.Setup(x => x.Multiplicity).Returns(Multiplicity.Multiple);

var p1 = new Mock<IPropertyInfo>();
p1.Setup(x => x.Id).Returns(Guid.NewGuid());
p1.Setup(x => x.Name).Returns("Foo" + Guid.NewGuid().ToString());
p1.Setup(x => x.Type).Returns("System.String");

var p2 = new Mock<IPropertyInfo>();
p2.Setup(x => x.Id).Returns(Guid.NewGuid());
p2.Setup(x => x.Name).Returns("Bar" + Guid.NewGuid().ToString());
p2.Setup(x => x.Type).Returns("System.String");

var elementInfoMock = new Mock<IElementInfo>();
elementInfoMock.Setup(e => e.Id).Returns(Guid.NewGuid());
elementInfoMock.Setup(e => e.Multiplicity).Returns(Multiplicity.Multiple);
elementInfoMock.Setup(e => e.Elements)
    .Returns(new List<IAbstractElementInfo>
    {
        el1.Object,
        c1.Object,
    });
elementInfoMock.Setup(x => x.Properties).Returns(
    new List<IPropertyInfo>
    {
        p1.Object,
        p2.Object,
    });

this.elementInfo = elementInfoMock.Object;

Same example using Mock.Of<T> (returns an instance of the class)

this.elementInfo = Mock.Of<IElementInfo>(x =>
x.Id == Guid.NewGuid() &&
x.Multiplicity == Multiplicity.Multiple &&
x.Elements == new List<IAbstractElementInfo>
{
    Mock.Of<IElementInfo>(e => e.Id == Guid.NewGuid() && e.Multiplicity == Multiplicity.Single),
    Mock.Of<ICollectionInfo>(e => e.Id == Guid.NewGuid() && e.Multiplicity == Multiplicity.Single),
} &&
x.Properties == new List<IPropertyInfo>
{
    Mock.Of<IPropertyInfo>(p => p.Id == Guid.NewGuid() && p.Name == "Foo" + Guid.NewGuid() && p.Type == "System.String"),
    Mock.Of<IPropertyInfo>(p => p.Id == Guid.NewGuid() && p.Name == "Foo" + Guid.NewGuid() && p.Type == "System.String"),
});

Solution 2

Based on the answers above, I guess when you mostly want to mock properties, Mock.Of<T>() is easier, whereas when you want to mock methods etc., Mock<T> is easier.

  • Mostly mocking properties:
var foo = Mock.Of<Foo>();
foo.Property1 = 1;
foo.Property2 = 2;
foo.Property3 = 3;
  • Mostly mocking methods:
var barMock = new Mock<Bar>();
barMock.Setup(bar => bar.GetValue1()).Returns(1);
barMock.Setup(bar => bar.GetValue2Async()).ReturnsAsync(2);

Solution 3

Both do the same but with Mock.Of it's more "natural" code.

Using Mock<T> looks more like this (or with Setup/Return like your code):

var mockService = new Mock<ISomeService>();
mockService.SetupProperty(s => s.IsActive);
mockService.Object.IsActive = true;
mockService.SetupProperty(s =>s.DelayTime);
mockService.Object.DelayTime = 5000;

Using Mock.Of<T> you get directly the instance and you can set the value you want. And you can use Mock.Get() to configure any method:

var mockService = Mock.Of<ISomeService>();
mockService.IsActive = true;
mockService.DelayTime = 5000;
Mock.Get(mockService).Setup(s => s.ExecuteTask()).Returns(true);

They do the same, but for more complex services with a lot of properties and methods, I think Mock.Of<T> is more readable

Share:
27,817
choopau
Author by

choopau

Updated on July 08, 2022

Comments

  • choopau
    choopau almost 2 years

    I'm currently confuse on how to mock.

    I'm using Moq. To mock objects I usually write this way

     var mockIRepo = new Mock<IRepo>();
    

    However, I need to create mock object for my setup.

    Option1 Is it better to mock my object which only contain properties this way?

     var object = Mock.Of<Object>()
    

    Option2 Or this way

     var object = new Mock<Object>()
    

    I've read that option 2 has setupproperties which is kinda questionable to me because I could also set the properties in option 1.

    Then what is the difference? Or is there a better way?

  • Artemious
    Artemious over 3 years
    Your example code for Mock.Of<>() only works if ISomeService has setters for IsActive and DelayTime. If it doesn't, declare like this: var mockService = Mock.Of<ISomeService>(mockService => mockService.IsActive == true && mockService.DelayTime == 5000);