Skip to main content
added 236 characters in body
Source Link
Dean OBrien
  • 1.8k
  • 1
  • 11
  • 23

I am in the process of setting up some unit tests for a Sitecore project (using this guide as a starting point: https://www.codeflood.net/blog/2020/05/17/logicless-view-itemless-model/)

I have come across a problem when trying to mock call MediaManager.GetMediaUrl(MediaItem).

This is the method I would like to test:

public string GetImageUrl(Item item, string fieldName, MediaUrlBuilderOptions options = null)
{
  if (item == null)
    return String.Empty;

  if(string.IsNullOrWhiteSpace(fieldName))
    return String.Empty;

  var imgField = (ImageField)item.Fields[fieldName];

  if (imgField != null && imgField.MediaItem != null)
  {
    return (options != null) ?
      HashingUtils.ProtectAssetUrl(MediaManager.GetMediaUrl(imgField.MediaItem, options)) :
      HashingUtils.ProtectAssetUrl(MediaManager.GetMediaUrl(imgField.MediaItem));
  }

  return String.Empty;
}

I have tried updating the code to pass in BaseMediaManager through DI:

public ImageTaxonomy(BaseMediaManager mediaManager)
{
    Assert.ArgumentNotNull(mediaManager, "mediaManager");
    _mediaManager = mediaManager;
}
...
_mediaManager.GetMediaUrl(imgField.MediaItem)

And then used the following nSubstitute code in my unit test:

var mediaItem = CreateMediaItem(database);
var mediaManager = Substitute.For<BaseMediaManager>();

mediaManager.GetMediaUrl(mediaItem).Returns("/a/path/to/an/image.jpg");

var sut = new ImageTaxonomy(mediaManager);

...
private Item CreateMediaItem(Database database = null)
{
    var definition = new ItemDefinition(mediaItemID, "Mock Media Item", ID.Null, ID.Null);
    var data = new ItemData(definition, Language.Current, Sitecore.Data.Version.First, new FieldList());
    var mediaItem = new Item(mediaItemID, data, database);
    return mediaItem;
}

However the call to _mediaManager.GetMediaUrl(imgField.MediaItem) always returns an empty string.

I am guessing the reason is, I shouldnt be mocking the BaseMediaManager class, as it doesnt contain virtual members? Even though this Sitecore page indicates that it should be virtual (https://doc.sitecore.com/xp/en/developers/93/sitecore-experience-manager/build-item-and-media-urls.html).

If that is indeed the case, how would you normally approach creating unit tests for cases that call the GetMediaUrl() function? I would have thought this is quite a common scenario to test.

Note: this is a continuation on from this previous question (which has more info on unit test being carried out): How to mock an ImageField in Sitecore using nSubstitute and xUnit

I am in the process of setting up some unit tests for a Sitecore project (using this guide as a starting point: https://www.codeflood.net/blog/2020/05/17/logicless-view-itemless-model/)

I have come across a problem when trying to mock call MediaManager.GetMediaUrl(MediaItem).

This is the method I would like to test:

public string GetImageUrl(Item item, string fieldName, MediaUrlBuilderOptions options = null)
{
  if (item == null)
    return String.Empty;

  if(string.IsNullOrWhiteSpace(fieldName))
    return String.Empty;

  var imgField = (ImageField)item.Fields[fieldName];

  if (imgField != null && imgField.MediaItem != null)
  {
    return (options != null) ?
      HashingUtils.ProtectAssetUrl(MediaManager.GetMediaUrl(imgField.MediaItem, options)) :
      HashingUtils.ProtectAssetUrl(MediaManager.GetMediaUrl(imgField.MediaItem));
  }

  return String.Empty;
}

I have tried updating the code to pass in BaseMediaManager through DI:

public ImageTaxonomy(BaseMediaManager mediaManager)
{
    Assert.ArgumentNotNull(mediaManager, "mediaManager");
    _mediaManager = mediaManager;
}
...
_mediaManager.GetMediaUrl(imgField.MediaItem)

And then used the following nSubstitute code in my unit test:

var mediaItem = CreateMediaItem(database);
var mediaManager = Substitute.For<BaseMediaManager>();

mediaManager.GetMediaUrl(mediaItem).Returns("/a/path/to/an/image.jpg");

var sut = new ImageTaxonomy(mediaManager);

...
private Item CreateMediaItem(Database database = null)
{
    var definition = new ItemDefinition(mediaItemID, "Mock Media Item", ID.Null, ID.Null);
    var data = new ItemData(definition, Language.Current, Sitecore.Data.Version.First, new FieldList());
    var mediaItem = new Item(mediaItemID, data, database);
    return mediaItem;
}

However the call to _mediaManager.GetMediaUrl(imgField.MediaItem) always returns an empty string.

I am guessing the reason is, I shouldnt be mocking the BaseMediaManager class, as it doesnt contain virtual members? Even though this Sitecore page indicates that it should be virtual (https://doc.sitecore.com/xp/en/developers/93/sitecore-experience-manager/build-item-and-media-urls.html).

If that is indeed the case, how would you normally approach creating unit tests for cases that call the GetMediaUrl() function? I would have thought this is quite a common scenario to test.

I am in the process of setting up some unit tests for a Sitecore project (using this guide as a starting point: https://www.codeflood.net/blog/2020/05/17/logicless-view-itemless-model/)

I have come across a problem when trying to mock call MediaManager.GetMediaUrl(MediaItem).

This is the method I would like to test:

public string GetImageUrl(Item item, string fieldName, MediaUrlBuilderOptions options = null)
{
  if (item == null)
    return String.Empty;

  if(string.IsNullOrWhiteSpace(fieldName))
    return String.Empty;

  var imgField = (ImageField)item.Fields[fieldName];

  if (imgField != null && imgField.MediaItem != null)
  {
    return (options != null) ?
      HashingUtils.ProtectAssetUrl(MediaManager.GetMediaUrl(imgField.MediaItem, options)) :
      HashingUtils.ProtectAssetUrl(MediaManager.GetMediaUrl(imgField.MediaItem));
  }

  return String.Empty;
}

I have tried updating the code to pass in BaseMediaManager through DI:

public ImageTaxonomy(BaseMediaManager mediaManager)
{
    Assert.ArgumentNotNull(mediaManager, "mediaManager");
    _mediaManager = mediaManager;
}
...
_mediaManager.GetMediaUrl(imgField.MediaItem)

And then used the following nSubstitute code in my unit test:

var mediaItem = CreateMediaItem(database);
var mediaManager = Substitute.For<BaseMediaManager>();

mediaManager.GetMediaUrl(mediaItem).Returns("/a/path/to/an/image.jpg");

var sut = new ImageTaxonomy(mediaManager);

...
private Item CreateMediaItem(Database database = null)
{
    var definition = new ItemDefinition(mediaItemID, "Mock Media Item", ID.Null, ID.Null);
    var data = new ItemData(definition, Language.Current, Sitecore.Data.Version.First, new FieldList());
    var mediaItem = new Item(mediaItemID, data, database);
    return mediaItem;
}

However the call to _mediaManager.GetMediaUrl(imgField.MediaItem) always returns an empty string.

I am guessing the reason is, I shouldnt be mocking the BaseMediaManager class, as it doesnt contain virtual members? Even though this Sitecore page indicates that it should be virtual (https://doc.sitecore.com/xp/en/developers/93/sitecore-experience-manager/build-item-and-media-urls.html).

If that is indeed the case, how would you normally approach creating unit tests for cases that call the GetMediaUrl() function? I would have thought this is quite a common scenario to test.

Note: this is a continuation on from this previous question (which has more info on unit test being carried out): How to mock an ImageField in Sitecore using nSubstitute and xUnit

Source Link
Dean OBrien
  • 1.8k
  • 1
  • 11
  • 23

How to mock MediaManager in Sitecore using nSubstitute and xUnit

I am in the process of setting up some unit tests for a Sitecore project (using this guide as a starting point: https://www.codeflood.net/blog/2020/05/17/logicless-view-itemless-model/)

I have come across a problem when trying to mock call MediaManager.GetMediaUrl(MediaItem).

This is the method I would like to test:

public string GetImageUrl(Item item, string fieldName, MediaUrlBuilderOptions options = null)
{
  if (item == null)
    return String.Empty;

  if(string.IsNullOrWhiteSpace(fieldName))
    return String.Empty;

  var imgField = (ImageField)item.Fields[fieldName];

  if (imgField != null && imgField.MediaItem != null)
  {
    return (options != null) ?
      HashingUtils.ProtectAssetUrl(MediaManager.GetMediaUrl(imgField.MediaItem, options)) :
      HashingUtils.ProtectAssetUrl(MediaManager.GetMediaUrl(imgField.MediaItem));
  }

  return String.Empty;
}

I have tried updating the code to pass in BaseMediaManager through DI:

public ImageTaxonomy(BaseMediaManager mediaManager)
{
    Assert.ArgumentNotNull(mediaManager, "mediaManager");
    _mediaManager = mediaManager;
}
...
_mediaManager.GetMediaUrl(imgField.MediaItem)

And then used the following nSubstitute code in my unit test:

var mediaItem = CreateMediaItem(database);
var mediaManager = Substitute.For<BaseMediaManager>();

mediaManager.GetMediaUrl(mediaItem).Returns("/a/path/to/an/image.jpg");

var sut = new ImageTaxonomy(mediaManager);

...
private Item CreateMediaItem(Database database = null)
{
    var definition = new ItemDefinition(mediaItemID, "Mock Media Item", ID.Null, ID.Null);
    var data = new ItemData(definition, Language.Current, Sitecore.Data.Version.First, new FieldList());
    var mediaItem = new Item(mediaItemID, data, database);
    return mediaItem;
}

However the call to _mediaManager.GetMediaUrl(imgField.MediaItem) always returns an empty string.

I am guessing the reason is, I shouldnt be mocking the BaseMediaManager class, as it doesnt contain virtual members? Even though this Sitecore page indicates that it should be virtual (https://doc.sitecore.com/xp/en/developers/93/sitecore-experience-manager/build-item-and-media-urls.html).

If that is indeed the case, how would you normally approach creating unit tests for cases that call the GetMediaUrl() function? I would have thought this is quite a common scenario to test.