Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • GerlyDC 1 post 21 karma points
    May 20, 2021 @ 13:20
    GerlyDC
    0

    UnitTesting UmbracoApiController NullReferenceException

    Hey guys, I have this problem and really could use some help/explination. I'm fairly new to Umbraco, unit testing and Mocking so bear with me please. I have this simple method in my UmbracoApiController that checks if a certain DocType allows variations. Now the problem lies with trying to test this method. I always recieve a nullReferenceException. I think it probably has something to do with bad Mocking, because I never seem te get the right data. If someone can point me in the right direction, that would be greatly appreciated.

    This is my API Controller:

      public class PersonalizationApiController: UmbracoAuthorizedApiController
        {
            private readonly IScopeProvider _scopeProvider;
            private readonly IContentTypeService _contentTypeService;
            public PersonalizationApiController(IScopeProvider scopeProvider, IContentTypeService contentTypeService)
            {
                _scopeProvider = scopeProvider;
                _contentTypeService = contentTypeService;
            }  
    [AllowAnonymous]
            [HttpGet]
            public IHttpActionResult CheckIfDocTypeEnabled(string alias)
            {
                try
                {
                    var ct = _contentTypeService.Get(alias);
                    if (ct.Variations.VariesBySegment())
                    {
                        return Ok(true);
                    }
                    else
                    {
                        return Ok(false);
                    }
                }
                catch (Exception e)
                {
                    return BadRequest($"Error occured: {e}");
                }
            }
    

    This is my test:

    [TestFixture]
        public class PersonalizationApiControllerTests : UmbracoBaseTest
        {
            private PersonalizationApiController controller;
            private Mock<IContentTypeService> _contentTypeService = new Mock<IContentTypeService>();
            [SetUp]
            public override void SetUp()
            {
                base.SetUp();
                controller = new PersonalizationApiController(Mock.Of<IScopeProvider>(),_contentTypeService.Object);
            } 
    [Test]
        public void WhenCheckIfDoctypeEnabledWithKnownDoctype_ThenReturnTrue()
        {
            //Arrange
            controller.Configuration = new HttpConfiguration();
            controller.Request = new System.Net.Http.HttpRequestMessage();
    
            //Act
            IHttpActionResult actionResult = controller.CheckIfDocTypeEnabled(ContentPage.ModelTypeAlias);
            var response = actionResult.ExecuteAsync(new System.Threading.CancellationToken());
            var result = JsonConvert.DeserializeObject<bool>(response.Result.Content.ReadAsStringAsync().Result);
    
    
            //Assert
            Assert.IsTrue(result);
        }
    

    Kind regards,

    Gerly

  • Gutkowski 1 post 71 karma points
    May 21, 2021 @ 09:23
    Gutkowski
    0

    Hello,

    I am wondering if you guys do unit test your controllers and if it is common practise in a TTD environment.

    What I have are skinny controllers that basically call on to the business layer for logic and data and then populate a local ViewModel to pass to the view.

    I do unit test the business layer but I wonder if testing these skinny controllers still make sense or its really not necessary unless you have a fat controller with a lot of logic in it.

    Please let me know your thoughts.

    Thanks.

    Kabosucash

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies