0

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 this the test pass OK
            var contactModel = new ContactModel(); //Here the Exception is thrown
            var response = controller.Contact(contactModel);
            Assert.IsNotNull(response);
        }

I 've checked some related content here and here too but no one with lucky. What can I do to solve this?

Thank you.

ArlanG
  • 888
  • 9
  • 21
  • You can't just create your controller because it needs tons of dependencies. Take a look at http://skrift.io/articles/archive/unit-testing-umbraco-with-umbraco-context-mock/ – Mario Lopez Nov 15 '17 at 00:24
  • Sorry @MarioLopez but I try this to and didn't work for me, my controllers inherits form SurfaceController. – ArlanG Nov 15 '17 at 15:10
  • Read the article, you have to download a couple of Nuget packages and inherit your controller from BaseSurfaceController instead. This controller has the constructors so you can pass mock context. – Mario Lopez Nov 15 '17 at 19:47
  • @MarioLopez An the models? they inherits from Umbraco.Web.Models.RenderModel and give me too null reference exception with constructor `:base(UmbracoContext.Current.PublishedContentRequest.PublishedContent){ }` – ArlanG Nov 15 '17 at 20:11
  • Also the PublishedContentRequest of the mock is always null. – ArlanG Nov 16 '17 at 18:15
  • I honestly don't know where you are getting that issue. If you follow that aricle everything is very clear. What models are you talking about? – Mario Lopez Nov 16 '17 at 21:48
  • Hi @MarioLopez, my ContactModel inherits from `Umbraco.Web.Models.RenderModel` and the constructor looks like: `public ContactModel() :base(UmbracoContext.Current.PublishedContentRequest.PublishedContent){ }` when I try to create my model instance or mock to pass it to the controller always get null ref exception and send me to the constructor. I use the approach [here](http://blog.aabech.no/archive/the-basics-of-unit-testing-umbraco-just-got-simpler/) to get it done and nothing, the same. – ArlanG Nov 17 '17 at 14:59
  • If you use the Gdev package, you have to inherit form BaseSurfaceController, then you pass a ContentMock and helper in the constructor. You don't have to pass your RenderModel to the controller but to the ActionMethods. Actually I don't think you can use Surface Controllers for this, you should use RenderMvcController. Are you trying to hijack the routes? : https://our.umbraco.org/documentation/reference/routing/custom-controllers – Mario Lopez Nov 17 '17 at 21:09
  • And the constructor for your ContactModel looks wrong, I don't think it should call that base. Where have you taken those instructions from? – Mario Lopez Nov 17 '17 at 21:10

0 Answers0