2

Is possible to autogenerate a Media "element" ?

I am trying to do this

mediaProvider.GetMedia(Arg.Is<Sitecore.Data.Items.MediaItem>(i => i.ID == mediaItemId)).Returns(Arg.Any<Media>());

but this can apparently not be done? I don't care about what the content of the Media, it just not allowed to be empty, I would very much like to avoid defined the media, for reusability purpose..

I was in the Sitecore chat pointed towards using AutoFixture, but I'm not quite sure how to use it here and whether it creates a specific instance of the Media, and not a random one.

2

There is a package from Sitecore.FakeDb that integrates with AutoFixture to generate items on the fly. The Nuget package is Sitecore.FakeDb.AutoFixture

https://github.com/sergeyshushlyapin/Sitecore.FakeDb/wiki/AutoFixture-Samples

After you generate the item, then you can assign the item as a MediaItem https://github.com/sergeyshushlyapin/Sitecore.FakeDb/wiki/Media

A very quick sample walkthrough will be something like:

[Fact]
public void CreateItemInstance()
{
    var fixture = new Fixture().Customize(new AutoDbCustomization()).Customize(new AutoContentItemCustomization()); ;
    var item = fixture.Create<Item>();
    var database = fixture.Create<Database>();

    Sitecore.Data.Items.MediaItem mediaItem = new MediaItem(database.GetItem(item.ID));

  // Continue doing the rest
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.