Minimum Version: 2.6.0 / 3.6.0
If you only need to unit test methods that either retrieve (download) or delete files, it might be handy having a quick way of initializing these files without uploading them using the three messages explained in the Uploading section.
Thankfully FakeXrmEasy has a nice and quick way to initialize files. The fake context has an .InitializeFiles() method where you can set the initial state of the In-Memory FileDb with a set of pre-existing files.
Here’s an example of how to do this, if you want to see the full sample code please take a look at here:
var _entity = new Entity(dv_test.EntityLogicalName)
{
Id = Guid.NewGuid(),
};
var _file = new FileAttachment()
{
Id = Guid.NewGuid().ToString(),
MimeType = "application/pdf",
FileName = "TestFile.pdf",
Target = _entity.ToEntityReference(),
AttributeName = FILE_ATTRIBUTE_NAME,
Content = new byte[] { 1, 2, 3, 4 }
};
_entity[FILE_ATTRIBUTE_NAME] = _file.Id;
_context.Initialize(_entity);
_context.InitializeFiles(new [] { _file });
In the above example we initialize a sample entity record. We need to do it because files are associated to a specific entity record (the Target).
Once that’s done, we then create a new FileAttachment instance, with several properties, like the actual blob content, mime type etc.
We need to also set both the Target property (an entity reference to the record), as well as the attribute name, this is the logical name of the file column.
Like records, the file must also have a unique id (Id) which we need to set, and that we’ll set to the entity record as well in it’s file column.
With the above example, we have initialized the state of the In-Memory database with a single entity record and a single file quite easily.