Home > Software design >  Mock interface with setting up list of data
Mock interface with setting up list of data

Time:08-31

public interface ICar
{
    public int Id {get;}
    public string Owner {get;} 
{

in the test I mock this interface

var car = new Mock<ICar>();
car.SetupGet(x=>x.Id).Returns(1);
car.SetupGet(x=>x.Name).Returns("Bob");

how can I populate list of mocked

var test = new Mock<List<ICar>>().Object.Add(car);

this doesn't work.

CodePudding user response:

"how can I populate list of mocked" as you would populate any other list. But you fill it with mocks

var car = new Mock<ICar>();
car.SetupGet(x=>x.Id).Returns(1);
car.SetupGet(x=>x.Name).Returns("Bob");

List<ICar> myList = new List<ICar>();
myList.Add(car.Object);

You can do this also in a loop:

for ( int i = 0; i < 10; i   )
{
    var car = new Mock<ICar>();
    car.SetupGet(x=>x.Id).Returns( i   1 );
    car.SetupGet(x=>x.Name).Returns($"Bob_{i}");
    myList.Add(car.Object);
}
  • Related