Search code examples
c#unit-testingnunitumbracoumbraco7

Setting up Mock IMember Object in Umbraco 7.5.10 for Unit Testing


So I'm trying to Unit Test an Umbraco 7.5.10 controller. Context has been mocked and passed through with no issue. I then go on to create a mock IMember object but whenever I try to set any of the properties (LastLoginDate, Name, Email etc) they're not being set and when you go to inspect the inactiveMemberEmailed.Object during debugging all the properties are null. I've checked what IMember is inheriting and there is no reason why they shouldn't be set.

using NUnit.Framework;
using Umbraco.Core.Models;
using Site.Logic.Controllers.SurfaceControllers;
using System;
using Moq;
using Umbraco.UnitTesting.Adapter.Support;
using System.Collections.Generic;

namespace Site.Tests
{
    [TestFixture]
    public class InactiveMemberCheckTest
    {
        private UmbracoSupport support = new UmbracoSupport();
        private Mock<IMember> inactiveMemberEmailed;

        [SetUp]
        public void Setup()
        {
            support.SetupUmbraco();

            this.inactiveMemberEmailed = new Mock<IMember>();


            inactiveMemberEmailed.Object.LastLoginDate = DateTime.Today.AddMonths(-12);
            inactiveMemberEmailed.Object.Name = "inactiveMemberNotEmailed";
            inactiveMemberEmailed.Object.Email = "email@example.co.uk";
            inactiveMemberEmailed.Object.SetValue("contactId", 1);
            inactiveMemberEmailed.Object.SetValue("inactiveCheckEmailed", true);
        }

        [Test]
        public void MembersCanBeChecked()
        {
            var controller = new InactiveMemberCheckController(support.UmbracoContext);

            var resultInactiveEmailed = controller.InactiveCheck(inactiveMemberEmailed.Object);

            Assert.AreEqual(resultInactiveEmailed, true);
        }
    }
}   

Solution

  • I have resolved my own issue; I hadn't setup the properties before setting them (I assumed they'd have been setup automatically when the object was initialised) so the line of code to resolve this is:

    inactiveMemberEmailed.SetupAllProperties();