DavidB
DavidB

Reputation: 2596

Unit testing entity frameworks identity id is zero

[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);
}

I am trying to unit test a data insert using entity frameworks. I have set up a mock of my data repository, and have set up a helper that creates a snapshot each time the repository is saved, that can track the changes.

My issue is that within the method being tested:

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);
  }
}

The Id for pt is always zero, I cant work out if ef is assiging zero as the first id or this is just the default value fior an integer. The AddQueue method throws an error if pt.id is zero, so my test fails.

Can anyone help?

Upvotes: 0

Views: 424

Answers (1)

aquaraga
aquaraga

Reputation: 4168

It is most definitely because the default value of integer is 0, since you've mocked the repository.

I think you also should mock the _queuesLogic as well, instead of using a real instance in your test method:

new PoliciesLogic_Accessor(po)._queueLogic = new QueuesLogic(dataFactory);//use a mock instead of doing a new

This way, you wouldn't have to worry about the id being zero or not.

Upvotes: 1

Related Questions