Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set property (type interface) without implementation

I'm creating some UnitTests and want to mock a service which returns a Thing object with a FileInformation property that implements a IFileInformation interface.

How do I fill in / mock the Thing without writing a implementation for this interface?

public interface IFileInformation
{
   string Name { get; set; }
}
public class Thing
{
   public IFileInformation FileInformation { get; set; }
}

I'm using Moq lib for my UnitTests.

like image 684
Ralf de Kleine Avatar asked Apr 07 '26 06:04

Ralf de Kleine


1 Answers

Ok, it was easier than I thought.

Using Moq to mock the interface.

Mock<IFileInformation> fileInformation = new Mock<IFileInformation>();
fileInformation.SetupGet(x => x.Name).Returns("whatever.txt");

Thing serviceResult = new Thing();
serviceResult.FileInformation = fileInformation.Object;
like image 95
Ralf de Kleine Avatar answered Apr 09 '26 22:04

Ralf de Kleine



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!