1

我正在用 Moq 做一些单元测试,我想做相当于:

Location location = new Location();
LocationTimes locationTimes = new LocationTimes();
location.LocationTimes.Add(locationTimes);

所以我尝试了这个:

var mockLocation = new Mock<Location>();
var mockLocationTime = new Mock<LocationTimes>();

mockLocationTime.SetupProperty(f => f.isEarlyHoursSupported, true);
mockLocationTime.SetupProperty(f => f.isEarlyHoursSupportedManual, true);

var mockLocationTimeList = new List<Mock<LocationTimes>>();
mockLocationTimeList.Add(mockLocationTime);

this.mockLocation1 = new Mock<Location>();
mockLocation1.SetupProperty(f => f.LocationTimes, mockLocationTimeList);

...但我在最后一行出现错误,因为 mockLocationTimeList 现在是一个起订量列表。

我该怎么做?

4

2 回答 2

1

这应该为您解决问题。

var mockLocation = new Mock<Location>(); 
var mockLocationTime = new Mock<LocationTimes>(); 

mockLocationTime.SetupProperty(f => f.isEarlyHoursSupported, true); 
mockLocationTime.SetupProperty(f => f.isEarlyHoursSupportedManual, true); 

var mockLocationTimeList = new List<LocationTimes>(); 
mockLocationTimeList.Add(mockLocationTime.Object); 

this.mockLocation1 = new Mock<Location>(); 
mockLocation1.SetupProperty(f => f.LocationTimes, mockLocationTimeList);

现在 theList真的是 aList<LocationTimes>但每个LocationTimes对象都是一个 mock。

于 2012-10-16T17:11:02.153 回答
0

我能让这个工作的唯一方法是这样的:

var mockLocation = new Mock<Location>();
var mockLocationTime = new Mock<LocationTimes>();

mockLocationTime.SetupProperty(f => f.isEarlyHoursSupported, true);
mockLocationTime.SetupProperty(f => f.isEarlyHoursSupportedManual, true);

this.mockLocation1 = new Mock<Location>();
mockLocation1.SetupProperty(f => f.LocationTimes, new List<LocationTimes>());
mockLocation1.Object.LocationTimes.Add(mockLocationTime.Object);
于 2012-10-17T09:37:21.667 回答