How to generate a fake list using Moq

I want to generate a fake list without specifying all the properties of a fake object using Moq:

var mock = Mock.Of<ICalendar>(x => x.GetSchedules() == new List<ISchedule> { // I don't want specify explicitly title and other properties Mock.Of<ISchedule>(y => y.Title == "fdfdf" && y.Start == DateTime.Today) }); List<ISchedule> s = mock.GetSchedules(); 

Is it possible to specify "rules" instead of hard code properties? And is it possible to set the number of elements I want?

Thanks.

+4
source share
3 answers

Hope this helps:

 int numberOfElements = 10; var mock = Mock.Of<ICalendar>(x => x.GetSchedules() == Enumerable.Repeat(Mock.Of<ISchedule>(), numberOfElements).ToList()); 
+2
source

Take a look at AutoMoq and see if this does what you want.

+2
source

You can create an abstract ScheduleMockBuilder class that creates a Mock from a schedule with some random data. Then call this builder as many times as you need on the list.

See the Builder Template for more information.

You can also use QuickGenerate ; This is a library with a generic developer that my colleague wrote. It can generate objects with random properties from the box, and you can even add restrictions to the generated random data.

+1
source

Source: https://habr.com/ru/post/1401773/


All Articles