Ralf de Kleine
Ralf de Kleine

Reputation: 11734

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.

Upvotes: 3

Views: 121

Answers (2)

Ralf de Kleine
Ralf de Kleine

Reputation: 11734

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;

Upvotes: 4

Leigh Ciechanowski
Leigh Ciechanowski

Reputation: 1317

If you don't want to create the interface, you have no option but to new up the Thing class in your unit tests.

var sut = new Thing();

Upvotes: -1

Related Questions