I'm using FakeDB to try and set up unit tests for a custom component.
While trying to run it, I walked into a couple of missing .dll's which I hadn't expected since the FakeDB install page hadn't mentioned them, but I added them to the references anyway, reasoning this is probably due to SXA. They were System.Web.MVC (logical I guess) and Sitecore.XA.Foundation.IoC (this is where it fails).
The latter one now throws an InvalidOperationException, telling me
No locator provider is configured at Sitecore.XA.Foundation.IoC.ServiceLocator.get_Current() at Sitecore.XA.Foundation.Mvc.Repositories.Base.ModelRepository.get_Rendering() at Sitecore.XA.Foundation.Mvc.Repositories.Base.ModelRepository.FillBaseProperties(Object m)
I'm aware FakeDB allows me to configure settings that may resolve this, but SXA documentation is still so scarce I couldn't find what to do. Can any of you help me figure this out?
Here's my code:
public class ReadSpeakLinkRepository : ModelRepository, IReadSpeakLinkRepository
{
public override IRenderingModelBase GetModel()
{
ReadSpeakLinkModel model = new ReadSpeakLinkModel();
FillBaseProperties(model); // test fails here
if (model.DataSourceItem == null || model.DataSourceItem.TemplateID != SitecoreItemConstants.TemplateId)
{
return null;
}
FillExtraProperties(model);
return model;
}
private void FillExtraProperties(ReadSpeakLinkModel model)
{
var options = LinkManager.GetDefaultUrlOptions();
options.AlwaysIncludeServerUrl = true;
model.CurrentUrl = LinkManager.GetItemUrl(Context.Item, options);
model.ReadText = model.DataSourceItem[SitecoreItemConstants.TextFieldSelector];
model.ReadTitle = model.DataSourceItem[SitecoreItemConstants.TitleFieldSelector];
}
}
And the test itself:
private ReadSpeakLinkRepository repository;
private Db fakeDb;
[TestInitialize]
public void Initialize()
{
fakeDb = new Db();
repository = new ReadSpeakLinkRepository();
}
[TestMethod]
public void TestMethod1()
{
fakeDb.Add(new DbItem("ReadSpeakLink") {
{ SitecoreItemConstants.TitleFieldSelector, "titel" },
{ SitecoreItemConstants.TextFieldSelector, "tekst" }
});
var result = repository.GetModel();
Assert.IsNotNull(result);
}
// plus IDisposable implementation