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?
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:
[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);
}
}
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?
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.
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:
line 43 refers to my ContactModel constructor (inherited from Umbraco.Web.Models.RenderModel ) and My Controllers are inherited from SurfaceController.
An this is my test method:
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.
I think you are missing the IPublishedContent in your example.
IPublishedContext get me a NullReferenceException.
This exception sends me to the ContactModel Constructor:
Still looks like you're missing IPublishedContent in your constructor. Thats why it throws a null exception.
This is all my Test Class:
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?
I found the solution! I have to initialize the ControllerContext.
After this, when I call my Model It looks great and the Test Passed like charm! Thanks to Shazwazza and his SurfaceControllerTests.
is working on a reply...