0

I am working on upgrading a project from Sitecore 8.2 to 9.3. Few tests are failing and throwing NullReferenceException at Sitecore.XA.Foundation.Mvc.Repositories.Base.FillBaseProperties(model).

While debugging, I saw that IsEdit and IsEditable properties are throwing exception. Can anybody please let me know the Unit Test code to assign values to these two parameters? Please see current test code below -

public void GetModel_ReturnsModel(IServiceProvider serviceProvider, IPageContext pageContext, IRendering rendering, IContentRepository contentRepository, IControlEditability controlEditability, IControlEditabilityService controlEditabilityService, IMultisiteContext multisiteContext, IPageMode pageMode)
        {
            // Arrange            
            serviceProvider.GetService(typeof(IPageMode)).Returns(pageMode);
            SnackbarNotificationRepository sut = new SnackbarNotificationRepository();
            sut.SetFieldValue("_rendering", rendering);
            sut.SetFieldValue("_pageContext", pageContext);
            sut.SetFieldValue("_contentRepository", contentRepository);
            sut.SetFieldValue("_controlEditability", controlEditability);
            sut.SetFieldValue("_controlEditabilityService", controlEditabilityService);
            sut.SetFieldValue("_multisiteContext", multisiteContext);
           
            FakeSiteContext fakeSite = new FakeSiteContext(
              new StringDictionary
                  {
                        { "name", "TestSite" },
                        { "rootPath", "/sitecore/content/test" },
                        { "startItem", "/Home" }
                  });
            IRenderingModelBase result = null;

            // Act
            using (new SiteContextSwitcher(fakeSite))
            { 
                result = sut.GetModel();
            }

            // Assert
            result.Should().BeOfType<SnackbarNotificationModel>();
        }

enter image description here

1
  • Mock these properties. You have already code snippets on your screenshot. Commented Apr 28, 2022 at 21:42

1 Answer 1

0

I was missing one line of code, that is to use the serviceProvider. I modified the above code as shown below and the test runs successfully now.

[Theory, AutoDbData]
    public void GetModel_ReturnsModel(IServiceProvider serviceProvider, IPageContext pageContext, IRendering rendering, IContentRepository contentRepository, IControlEditability controlEditability, IControlEditabilityService controlEditabilityService, IMultisiteContext multisiteContext)
    {
        // Arrange         
        PageMode pageMode = Substitute.For<PageMode>();
        serviceProvider.GetService(typeof(IPageMode)).Returns(pageMode);
        IRenderingModelBase result = null;

        // Act
        using (new ServiceProviderSwitcher(serviceProvider)) //Added this line of code
        {
            SnackbarNotificationRepository sut = new SnackbarNotificationRepository();
            sut.SetFieldValue("_rendering", rendering);
            sut.SetFieldValue("_pageContext", pageContext);
            sut.SetFieldValue("_contentRepository", contentRepository);
            sut.SetFieldValue("_controlEditability", controlEditability);
            sut.SetFieldValue("_controlEditabilityService", controlEditabilityService);
            sut.SetFieldValue("_multisiteContext", multisiteContext);
            result = sut.GetModel();
        }

        // Assert
        result.Should().BeOfType<SnackbarNotificationModel>();
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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