I am newer to Sitecore and extremely new to its unit testing. been following examples online that mimic exactly what I am trying to accomplish but I seem to be getting an object reference(you will see the error in the screenshots) not set to an instance for an item that I am passing through a function on the unit test and in the function. We had other contracted Sitecore developers on the project in the past write unit tests the same way I am here but their unit tests are passing while throwing that object reference error from their tests. I am working on a Sitecore 10.1 project solution, not that it matter in this situation i think. I am attaching screenshots for clarity on the errors and stack traces. Is this normal for how the unit tests are supposed to be working with Nsubstitute and Sitecore, maybe I am missing the point with how Sitecore works with Unit tests. Please let me know your thoughts :)
private Item CreateItem(Database database = null)
{
var db = database ?? Substitute.For<Database>();
var item = Substitute.For<Item>(ID.NewID, ItemData.Empty, db);
var fields = Substitute.For<FieldCollection>(item);
item.Fields.Returns(fields);
db.GetItem(item.ID).Returns(item);
db.GetItem(item.ID.ToString()).Returns(item);
return item;
}
private void SetItemField(Item item, string fieldName, string fieldValue)
{
item[fieldName].Returns(fieldValue);
var field = Substitute.For<Field>(ID.NewID, item);
field.Database.Returns(item.Database);
field.Value = fieldValue;
item.Fields[fieldName].Returns(field);
}
It seems to be something wrong with how I am setting up the unit tests itself but I have included the whole run through for clarity. If you have any other suggestions for unit testing with Sitecore, I am very open to learning new things with Sitecore :)