[TestMethod()]
[Description("Policies: AddReconciliationForDevice")]
public void PL210102()
{
// arrange
IDeviceDataFactory dataFactory = new DeviceDataFactoryMock();
PoliciesLogic target = new PoliciesLogic(dataFactory);
var po = new PrivateObject(target);
new PoliciesLogic_Accessor(po)._queueLogic = new QueuesLogic(dataFactory);
DeviceDataRepositoryMock repository = dataFactory.Create() as DeviceDataRepositoryMock;
DeviceEntitiesMock model = repository.Entities;
var changeHelper = new SaveChangesHelper(model);
//Act
target.AddReconciliationForDevice(1, new DateTime(2010,01,01));
// assert;
Assert.AreEqual(2, changeHelper.Snapshots.Count());
Assert.AreEqual(1, changeHelper.Snapshots[1].PackageTemplates.Count());
Assert.AreEqual("Reconcile", changeHelper.Snapshots[1].PackageTemplates.ElementAt(0).Name);
Assert.AreEqual((int)EnumPackageItemTemplatesCommand.ReconcileData, changeHelper.Snapshots[1].PackageItemTemplates.ElementAt(0).CommandTypeId);
Assert.AreEqual(1, changeHelper.Snapshots[1].Packages.Count());
Assert.AreEqual(1, changeHelper.Snapshots[1].PackageItems.Count());
Assert.AreEqual(1, changeHelper.Snapshots[1].Queues.Count());
Assert.AreEqual(new DateTime(2010, 01, 01), changeHelper.Snapshots[1].Queues.ElementAt(0).EarliestExecutionTime);
}
我正在尝试使用实体框架对数据插入进行单元测试。我已经建立了我的数据存储库的模拟,并设置了一个帮助器,它在每次保存存储库时创建一个快照,可以跟踪更改。
我的问题是在正在测试的方法中:
public void AddReconciliationForDevice(int deviceId, DateTime timeToSend)
{
if (deviceId <= 0) throw new ArgumentException(String.Format(CultureInfo.InvariantCulture, "Invalid deviceId value '{0}' seen", deviceId));
using (IDeviceData repository = DataFactory.Create())
{
List<int> devList = new List<int>();
devList.Add(deviceId);
// Add PackageTemplate
var pt = AddPackageTemplate("Reconcile", 28, repository);
// Add PackageItemTemplate
var pit = AddPackageItemTemplate(pt, null, repository, EnumPackageItemTemplatesCommand.ReconcileData, 1);
// Add package
List<Package> packList = AddPackage(pt, devList, repository, false);
// Add PackageItem
AddPackageItem(pit, packList.ElementAt(0), repository);
// Saves to get packageTemplateId
repository.SaveChanges();
// Adds queue
_queueLogic.AddQueue(pt.Id, deviceId, timeToSend);
}
}
Id
forpt
始终为零,我无法确定 ef 是否将零指定为第一个 id,或者这只是整数的默认值。AddQueue
如果 pt.id 为零,该方法将引发错误,因此我的测试失败。
任何人都可以帮忙吗?