Try our conversational search powered by Generative AI!

Unit testing - how to add blocks to a content area in a test project

Vote:
 

Hi there, 

We would like to unit test a validator that ensures a content area doesn't have two blocks inside it: 

public class LandingPageValidator : IValidate
    {
        public IEnumerable Validate(LandingPageDataModel instance)
        {
            if (instance.HeroBlock.Count > 1)
            {
                return new ValidationError[]
                {
                    new ValidationError()
                    {
                        PropertyName = "Hero Block",
                        ErrorMessage = "Can only have one Hero Block"
                    }};
            }

            return Enumerable.Empty();
        }
    }

The problem we are facing is: how can we programatically add a second block to a content area, while inside the test project?

We have tried this approach: https://stackoverflow.com/questions/27993939/episerver-add-block-to-a-content-area-programmatically but that seems to work only in the main application (ContentReference.GlobalBlockFooter is null in the test project). 

In case it helps, here is the data model. 

public class LandingPageDataModel : BasePageDataModel
    {
        [Display(
            Name = "Hero block",
            Description = "",
            Order = 140,
            GroupName = SystemTabNames.Content)]
        [AllowedTypes(new[] { typeof(LandingPageHeroDataModel) })]
        public virtual ContentArea HeroBlock { get; set; }
    }

And here is what we have tried:

        [Fact]
        public void Validate_WhenHasMoreThanOneHeroBlock_ReturnsError()
        {
            // Arrange
            var validator = new LandingPageValidator();
            var model = new LandingPageDataModel { HeroBlock = new ContentArea() };
            var item = new ContentAreaItem();
            model.HeroBlock.Items.Add(item);
            var expected = new ValidationError[]
                {
                    new ValidationError()
                    {
                        PropertyName = "Hero Block",
                        ErrorMessage = "Can only have one Hero Block"
                    }};
            
            // Act
            var result = validator.Validate(model);
            
            // Assert
            Assert.Equal(expected, result);
        }

However, it throws a null reference exception when we try to add the ContentAreaItem to the ContentArea (using model.HeroBlock.Items.Add). The exact error is:

System.NullReferenceException

Object reference not set to an instance of an object.
at EPiServer.Core.ContentArea.AddContentAreaItemsToFragments(IList items, Int32 startIndex)
#189244
Edited, Mar 14, 2018 8:23
Vote:
 

Did you read this post? http://www.meadow.se/wordpress/unit-testing-an-episerver-contentarea/

#189679
Mar 22, 2018 23:30
Vote:
 

Thanks @Johan! We had not read that. Will give it a go and update here. 

#190543
Apr 11, 2018 18:08
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.