Unit testing in Sitecore is a tricky one since you need create an items in Fake dB. I didn’t find much information about Media items unit testing and below piece of code may help you!

  const string MediaURL = "~/media/image.ashx";
  Sitecore.Data.ID mediaID = Sitecore.Data.ID.NewID;

  // create media item first
  using (Sitecore.FakeDb.Db db = new Sitecore.FakeDb.Db
    new Sitecore.FakeDb.DbItem("image", mediaID )
    Sitecore.Data.Items.Item mediaItem = db.GetItem(mediaID );

    // create media provider mock and configure behaviour
    Sitecore.Resources.Media.MediaProvider mediaProvider =

      .GetMediaUrl(Arg.Is<Sitecore.Data.Items.MediaItem>(i => i.ID == mediaID ))

    // substitute the original provider with the mocked one
    using (new Sitecore.FakeDb.Resources.Media.MediaProviderSwitcher(mediaProvider))
      string mediaUrl = Sitecore.Resources.Media.MediaManager.GetMediaUrl(mediaItem);
      Xunit.Assert.Equal(MediaURL, mediaUrl);

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts

Fake DB

PageContext item in Sitecore Fake DB + Unit test cases

I tried to set PageContext using  RenderingContext.EnterContext but didn’t work using (RenderingContext.EnterContext(new Rendering(), contextItem)) { I have set it using  ContextService.Get().Push(pageContext); Here is the code, which may help you using (Db db = new Db using (Db Read more…

Sitecore 8.2

Mocking a reference field in Sitecore Fake DB for Unit test cases

I’m working on Unit test cases and mocking a reference field could be a complex task. Here is the code, which may help you [Fact] public void ReferenceFieldMockingTest() { ID newTemplateId = ID.NewID; ID referenceItemId Read more…


how to enable full rich text editor in sitecore

Add below paths in datasource location /sitecore/system/Settings/Html Editor Profiles/Rich Text Default /sitecore/system/Settings/Html Editor Profiles/Rich Text Full /sitecore/system/Settings/Html Editor Profiles/Rich Text IDE /sitecore/system/Settings/Html Editor Profiles/Rich Text Medium