Copied to clipboard

Flag this post as spam?

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


  • Arlan Galvez 44 posts 175 karma points
    Nov 14, 2017 @ 21:28
    Arlan Galvez
    0

    Testing Umbraco7.6.3 controller with model inherited from Umbraco.Web.Models.RenderModel

    I have a serious issue with testings on Umbraco 7.6.3 because my test always get the same error:

    System.NullReferenceException : Object reference not set to an instance of an object.
    
       at DirectUmbraco.Models.ContactModel..ctor() in E:\Projects\Umbraco\DirectUmbraco\DirectUmbraco\Models\ContactModel.cs:line 13
       at DirectUmbraco.Tests.FormsControllerTests.TestContactMethod() in E:\Projects\Umbraco\DirectUmbraco\DirectUmbraco.Tests\ControllersTests.cs:line 43
    

    line 43 refers to my ContactModel constructor (inherited from Umbraco.Web.Models.RenderModel ) and My Controllers are inherited from SurfaceController.

    public ContactModel()
                :base(UmbracoContext.Current.PublishedContentRequest.PublishedContent){
            }
    

    An this is my test method:

    [Test]
            public void TestContactMethod()
            {
                var controller= new FormController(); //until here the test pass OK
                var contactModel = new ContactModel(); //The Exception is thrown
                var response = controller.Contact(contactModel);
                Assert.IsNotNull(response);
            }
    

    I 've checked some related content here , here, here and here too but all with no lucky. My controllers inherits from Surface controller, they need too an UmbracoContext but how can I load all this contexts before make any test?

    Thank you.

  • Frans de Jong 550 posts 1862 karma points MVP 4x c-trib
    Nov 17, 2017 @ 15:28
    Frans de Jong
    1

    I think you are missing the IPublishedContent in your example.

    var contactModel = new ContactModel( IPublishedContent );
    
  • Arlan Galvez 44 posts 175 karma points
    Nov 17, 2017 @ 16:10
    Arlan Galvez
    0

    IPublishedContext get me a NullReferenceException.

    System.NullReferenceException : Object reference not set to an instance of an object.
    
       at Umbraco.Web.Models.RenderModel..ctor(IPublishedContent content)
    

    This exception sends me to the ContactModel Constructor:

    protected ContactModel(IPublishedContent content) : base(content) { }
    
  • Frans de Jong 550 posts 1862 karma points MVP 4x c-trib
    Nov 17, 2017 @ 16:15
    Frans de Jong
    0

    Still looks like you're missing IPublishedContent in your constructor. Thats why it throws a null exception.

  • Arlan Galvez 44 posts 175 karma points
    Nov 17, 2017 @ 16:25
    Arlan Galvez
    0

    This is all my Test Class:

     [TestFixture]
            public class FormsTests
            {
                private ContextMocker _mocker;
                private Mock<IPublishedContent> _currentPage;
    
                [SetUp]
                public void Setup()
                {
                    _mocker = new ContextMocker();
                    _currentPage = new Mock<IPublishedContent>();
                }
    
                [Test]
                public void Can_Return_Correct_Response()
                {
                   _currentPage.Setup(x => x.Id).Returns(1000);
    
                    var helper = new UmbracoHelper(this._mocker.UmbracoContextMock, this._currentPage.Object);
                    var formController = new FormsController(_mocker.UmbracoContextMock,helper);
    
                   var model = new ContactModel(_currentPage.Object); //here I get the null Reference exception. 
    
                    var result = formController.ContactForm(model);
    
                    Assert.IsNotNull(result);
                }
            }
    
  • Frans de Jong 550 posts 1862 karma points MVP 4x c-trib
    Nov 17, 2017 @ 16:37
    Frans de Jong
    0

    I have no experience with unittesting but It looks like the ContactModel is expecting a property in _currentPage.Object thats not present.

    What I would do is compare the properties in _currentPage.Object and compare it to the ContactModel and see if there is a non nullable property missing?

  • Arlan Galvez 44 posts 175 karma points
    Nov 17, 2017 @ 20:50
    Arlan Galvez
    100

    I found the solution! I have to initialize the ControllerContext.

     var contextBase = umbCtx.HttpContext;
                var pcr = new PublishedContentRequest(new Uri("http://localhost/test"),
                    umbCtx.RoutingContext,
                    webRoutingSettings,
                    s => Enumerable.Empty<string>())
                {
                    PublishedContent = content
                };
    
                var routeDefinition = new RouteDefinition
                {
                    PublishedContentRequest = pcr
                };
    
                var routeData = new RouteData();
                routeData.DataTokens.Add("umbraco-route-def", routeDefinition);
    
                var ctrl = new FormsController(umbCtx, new UmbracoHelper());
                ctrl.ControllerContext = new ControllerContext(contextBase, routeData, ctrl);
    

    After this, when I call my Model It looks great and the Test Passed like charm! Thanks to Shazwazza and his SurfaceControllerTests.

Please Sign in or register to post replies

Write your reply to:

Draft